Excel Training

Translate into language of your choice

Automated drawing using nested for loops


Complete code

Sub myfundrawing()
Rows.RowHeight = 8
Columns.ColumnWidth = 1
For Row = 5 To 13
For Column = 10 To 18
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.



Reference

Automated drawing using nested for loops