Automated Drawing in Excel Using Loops

Here is the complete VBA code with remarks:

Sub myfundrawing()
‘adjust rowheight
Rows.RowHeight = 8
‘adjust column width
Columns.ColumnWidth = 1
‘start the outer loop
For Row = 5 To 13
‘start the inner loop
For Column = 10 To 18
‘change the fill color
Cells(Row, Column).Interior.ColorIndex = 1
Next Column
Next Row
For Row = 14 To 16
For Column = 13 To
Cells(Row, Column).Interior.ColorIndex = 5
Next Column
Next Row
For Row = 17 To 26
For Column = 7 To 21
Cells(Row, Column).Interior.ColorIndex = 3
Next Column
Next Row
For Row = 27 To 37
For Column = 8 To 9
Cells(Row, Column).Interior.ColorIndex = 5
Next Column
Next Row
For Row = 27 To 37
For Column = 19 To 20
Cells(Row, Column).Interior.ColorIndex = 5
Next Column
Next Row
For Row = 36 To 37
For Column = 6 To 7
Cells(Row, Column).Interior.ColorIndex = 1 Next Column
Next Row
For Row = 36 To 37
For Column = 17 To 18
Cells(Row, Column).Interior.ColorIndex = 1
Next Column
Next Row
End Sub

The idea is to also demonstrate the nested for loop which can be used to create automated multi-colored drawings like a chessboard. The inner loop can be used to manipulate more colors.

View the fun video:


Further reading:
How to Loop in Excel

Leave a Reply

Your email address will not be published. Required fields are marked *