(预期表达式)vba错误

时间:2018-07-27 15:51:31

标签: excel vba excel-vba

所以基本上我的代码是这样的:

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

2 个答案:

答案 0 :(得分:2)

mod运算符的用法类似于5 mod 2 = 16 mod 2 = 0VBA 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中也是一种好习惯-因此,应声明变量ij。 -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