Visual Basic CHESS TABLE help

Jovan123

New Member
Joined
Jan 3, 2014
Messages
9
Hi, can you please help me with this task
I want to write a program that takes input of drawing chessboard n x n in excel table (and colors every other field black)

I wrote this code that works:
Option ExplicitSub main()
Dim n As Integer
Dim x As Integer
Dim y As Integer
n = InputBox("Enter number")

For x = 1 To n
For y = 1 To n
Cells(1, 1).Offset(x - 1, y - 1).Interior.ColorIndex = (x + y) Mod 2 + 1
Next y
Next x
End Sub


My question is how i can do this without Offset(x - 1, y - 1).Interior.ColorIndex = (x + y) Mod 2 + 1
and with this code Cells(x,y). Interior.Color = RGB(o,o,o) for each color (black and white)?

Thanks! :)
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,

Not exactly what you're after, but this could help

Code:
Sub a()
Dim iNumber As Integer
Dim rBoard As Range, rCell As Range
 
iNumber = InputBox("")

Set rBoard = Range(Cells(1, 1), Cells(iNumber, iNumber))

For Each rCell In rBoard
If Application.WorksheetFunction.IsOdd(rCell.Row + rCell.Column) = True Then rCell.Interior.Color = RGB(255, 255, 255) Else rCell.Interior.Color = RGB(0, 0, 0)
Next

End Sub
 
Upvote 0
You could use something like:
Code:
   For x = 1 To n
      For y = 1 To n
         Cells(x, y).Interior.Color = Choose((x + y) Mod 2 + 1, RGB(0, 0, 0), RGB(255, 255, 255))
      Next y
   Next x
 
Upvote 0
Perhaps
Code:
Dim i As Long, j As Long
Dim cellColor As Long
For i = 1 To 8
    For j = 1 To 8
        cellColor = RGB(255, 255, 255) * ((i + j) Mod 2)
        Cells(i, j).Interior.Color = cellColor
    Next j
Next i
or, if that is wrong way round
Code:
cellColor = RGB(255, 255, 255) * ((i + j + 1) Mod 2)
 
Upvote 0
Can you explain me what this part means or does "Choose((x + y) Mod 2 + 1" ? i`m absolute begginer
 
Upvote 0
(x + y) Mod 2
returns either 0 or 1 depending on whether the column + row is even or odd.
Adding 1 returns either 1 or 2 and the Choose function uses this number to return either the first or second RGB colour value.
 
Upvote 0

Forum statistics

Threads
1,214,432
Messages
6,119,468
Members
448,900
Latest member
Fairooza

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