小福利,用Excel vba设计一个变色小游戏
Option Explicit
Private Sub CommandButton1_Click()
Dim i, j
For i = 1 To 100
For j = 1 To 100
If i >= j Then
If i Mod 2 = 1 And j Mod 2 = 1 Then
Cells(i, j) = "zpsg"
With Cells(i, j).Interior
.Pattern = xlSolid
.Color = vbGreen
End With
Else
Cells(i, j) = i * j
End If
Else
Cells(i, j) = ""
End If
Next j
Next i
End Sub
Private Sub CommandButton2_Click()
[a1:cv100] = ""
[a1:cv100].Interior.Color = vbWhite
End Sub
Private Sub CommandButton3_Click()
Dim i, j
For i = 1 To 100
For j = 100 To 1 Step -1
If i <= j Then
If i Mod 2 = 1 And j Mod 2 = 1 Then
Cells(i, j) = "zpsg"
With Cells(i, j).Interior
.Pattern = xlSolid
.Color = vbRed
End With
Else
Cells(i, j) = i * j
End If
Else
Cells(i, j) = ""
End If
Next j
Next i
End Sub
Private Sub CommandButton4_Click()
Dim i, j
For i = 1 To 100
For j = 1 To 100
If i Mod 2 = 1 And j Mod 2 = 1 Then
Cells(i, j) = "zpsg"
With Cells(i, j).Interior
.Pattern = xlSolid
.Color = vbCyan
End With
Else
Cells(i, j) = i * j
End If
Next j
Next i
End Sub
Private Sub CommandButton5_Click()
Dim i, j
For i = 1 To 100
For j = 1 To 100
If i Mod 2 = 0 And j Mod 2 = 0 Then
Cells(i, j) = "zpsg"
With Cells(i, j).Interior
.Pattern = xlSolid
.Color = vbBlue
End With
Else
Cells(i, j) = i * j
End If
Next j
Next i
End Sub
Private Sub CommandButton6_Click()
Dim i, j
Dim r1
For i = 1 To 100 Step 3
For j = 1 To 100 Step 3
If i Mod 2 = 1 And j Mod 2 = 1 Then
Set r1 = Range(Cells(i, j), Cells(i, j + 1))
r1.Value = "zpsg"
With r1.Interior
.Pattern = xlSolid
.Color = RGB(255, 0, 255)
End With
Else
Cells(i, j) = i * j
End If
Next j
Next i
End Sub
如上图插入六个命令框,分别写入代码
效果展示如下