chessboard with vba

Ort1z98

New Member
Joined
Jun 27, 2013
Messages
7
hey everyone

I am trying to create a chessboard with a macro from vba. I have to use only for to of ifs to create the macro.

Sub macro6()
For column = 1 To 8
Cells(column, 1).Interior.Color = Black
Next column
End Sub
That is what i have so far. I know is lame but I dont know what to do next
Please help
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Maybe
Code:
Sub MM1()
For c = 1 To 8
    For r = 1 To 8
        Cells(1, 1).Offset(r - 1, c - 1).Interior.ColorIndex = (r + c) Mod 2 + 1
    Next r
Next c
End Sub
 
Upvote 0
Another way...
Code:
Sub MakeChessBoard()
  With Intersect(Range("A:A,C:C,E:E,G:G").EntireColumn, Range("1:1,3:3,5:5,7:7").EntireRow)
    .Interior.ColorIndex = 1
    .Offset(1, 1).Interior.ColorIndex = 1
  End With
End Sub
 
Upvote 0
hey thanks everyone the first one looks fine but I haven´t seen mod in my class yet. Can i substitute that with something else?
 
Upvote 0
Thanks for your answer man. Can you help me with this

Sub MM1()
For c = 1 To 8
For r = 1 To 8
Cells(1, 1).Offset(r - 1, c - 1).Interior.ColorIndex = (r + c) Mod 2 + 1
Next r
Next c
End Sub

The answer is ok but i haven´t seen mod in my class yet. I have to use for to and it seems that the answer includes if. Can you please help me with this, its a homework that i need to do for today

Thanks :)
 
Upvote 0
BY using for and if loop u can create

Sub chessboard()
For Col = 1 To 8
If Col Mod 2 = 1 Then
For Row = 2 To 8 Step 2


Cells(Row, Col).Interior.Color = 255
Next Row


Else
For Row = 1 To 8 Step 2
Cells(Row, Col).Interior.Color = 255
Next Row
End If
Next Col
End Sub
 
Upvote 0
Maybe
Code:
Sub MM1()
For c = 1 To 8
    For r = 1 To 8
        Cells(1, 1).Offset(r - 1, c - 1).Interior.ColorIndex = (r + c) Mod 2 + 1
    Next r
Next c
End Sub


I think without using offset we can write like this

Sub MM1()
For c = 1 To 8
For r = 1 To 8
Cells(r, c).Interior.ColorIndex = (r + c) Mod 2 + 1
Next r
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,123
Members
452,546
Latest member
Rafafa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top