-2

so basically my code goes like this:

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
Vityata
  • 42,633
  • 8
  • 55
  • 100
tehcoder
  • 25
  • 1
  • 7

1 Answers1

2

The mod operator is used like this 5 mod 2 = 1 and 6 mod 2 = 0.VBA equivalent to Excel's mod function.

Thus, it should be i Mod SomeNumber = 0 for the condition.

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 is also a good practice in VBA - thus your variables i and j should be declared. - VBA: problems with defining variables in function if Option Explicit is used

Vityata
  • 42,633
  • 8
  • 55
  • 100