create checkerboard with alternating patterns

Plotweaver

New Member
Joined
Jan 28, 2018
Messages
23
I'm creating a video game that uses an 8 by 8 grid. I need half of the 64 cells to be black and half white. I'm using conditional formatting to highlight the cells white or black. I've got a randomizer to randomly make the cells either black or white. But, I can't figure out a way so that - no matter the pattern - half are white and half are black. Can you help me devise a formula that will do this? Thanks in advance.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi and welcome to the forum.

This will produce a 32 random black squares in the range B2:I9:

Code:
Sub FillGrid()[INDENT]Randomize[/INDENT]
[INDENT]Dim rw as Long, cl as Long, i as Long
i = 1
Do[/INDENT]
[INDENT=2]rw = Int((8 - 1 + 1) * Rnd + 1) + 1
cl = Int((8 - 1 + 1) * Rnd + 1) + 1[/INDENT]
[INDENT=2]If Cells(rw, cl).Interior.Color <> RGB (0, 0, 0) Then[/INDENT]
[INDENT=3]Cells(rw, cl).Interior.Color = RGB (0, 0, 0)
i = i + 1[/INDENT]
[INDENT=2]End If[/INDENT]
[INDENT]Loop Until i = 33[/INDENT]
End Sub[INDENT]
[/INDENT]
[INDENT]
[/INDENT]

To clear the grid:

Code:
Sub ClearGrid()[INDENT]Range("B2:I9").Interior.Color = xlNone[/INDENT]
End Sub
 
Last edited:
Upvote 0
Code:
Sub FillGrid()
Randomize
Dim rw as Long, cl as Long, i as Long
i = 1
Do
rw = Int((8 - 1 + 1) * Rnd + 1) + 1
cl = Int((8 - 1 + 1) * Rnd + 1) + 1
If Cells(rw, cl).Interior.Color <> RGB (0, 0, 0) Then
Cells(rw, cl).Interior.Color = RGB (0, 0, 0)
i = i + 1
End If
Loop Until i = 33
End Sub

Code:
Sub ClearGrid()
Range("B2:I9").Interior.Color = xlNone
End Sub

Small code boxes, which I couldn't change, for some reason!
 
Upvote 0
Edit:

To stop the program crashing I forgot to put the call to clear the grid 1st!

Code:
Sub FillGrid()
    ClearGrid
    Randomize
    Dim rw As Long, cl As Long, i As Long
    i = 1
    Do
        rw = Int((8 - 1 + 1) * Rnd + 1) + 1
        cl = Int((8 - 1 + 1) * Rnd + 1) + 1
        If Cells(rw, cl).Interior.Color <> RGB(0, 0, 0) Then
            Cells(rw, cl).Interior.Color = RGB(0, 0, 0)
            i = i + 1
        End If
    Loop Until i = 33
End Sub


Sub ClearGrid()
    Range("B2:I9").Interior.Color = xlNone
End Sub
 
Upvote 0
Thanks. I'll try it and see if it accomplishes what I have in mind.

My ideal method would be to randomly select one of the 64 cells. Then move it to a new location. Meanwhile the cell in the new location is being moved to yet a third location... and so on. Just make sure the randomly selected cell has not been move previously. A literal 'shuffle' of cells.
 
Upvote 0
You could use conditional formatting with the formula =ISEVEN(ROW()+COLUMN())
I know what the thread title says, but if you read Message #1 carefully, I think you will see the OP is not looking for a regular checkerboard pattern, rather, he seems to be asking for half of a the squares on a checkerboard to be randomly selected and colored (his problem appears to be limiting the randomizing selection to 32 non-repeating squares).
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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