所以基本上我的代码是这样的:
Sub clor2()
For j = 0 To 10
For i = 0 To 10
if i mod = 0 and and j mod=0 then
[D2].Offset(j, i).Interior.ColorIndex = 37
Else
[D2].Offset(j, i).Interior.ColorIndex = 36
End If
Next i
Next j
End Sub
答案 0 :(得分:2)
mod
运算符的用法类似于5 mod 2 = 1
和6 mod 2 = 0
。VBA equivalent to Excel's mod function。
因此,该条件应为i Mod SomeNumber = 0
。
Option Explicit
Sub clor2()
Dim j As Long, i As Long
For j = 0 To 10
For i = 0 To 10
If i Mod 3 = 0 And j Mod 7 = 0 Then
[D2].Offset(j, i).Interior.ColorIndex = 37
Else
[D2].Offset(j, i).Interior.ColorIndex = 36
End If
Next i
Next j
End Sub
Option Explicit
在VBA中也是一种好习惯-因此,应声明变量i
和j
。 -VBA: problems with defining variables in function if Option Explicit is used
答案 1 :(得分:0)
尝试一下
Sub clor2()
For j = 0 To 10
For i = 0 To 10
If i Mod [missing number i is being modded by] = 0 And j Mod [missing number i is being modded by] = 0 Then
Range("D2").Offset(j, i).Interior.ColorIndex = 37
Else
Range("D2").Offset(j, i).Interior.ColorIndex = 36
End If
Next i
Next j
End Sub