Build a maze programmatically in Excel
Table of Contents
1. Build a maze programmatically in Excel
The image above shows the creation of a maze located on a worksheet. A macro builds this maze randomly, a new version is generated each time you start the macro.
Two weeks ago I posted a link to a workbook containing formulas calculating the shortest path in a maze. Today I have created a macro that builds a random maze.
The cell grid is 110 x 110 and the start cell is randomly chosen and colored yellow. The macro then randomly creates a path until it can´t move further. The endpoint is a cell with the longest distance to the start cell, colored blue.
The path between them is usually around 2500 cells. You can find the blue cell somewhere in the upper left corner, at the end of the animation. There is only one possible path between the start cell and end cell.
The animated image below shows the macro creating the maze, you can see how the macro creates new paths when there is nowhere to go.
To start building a new maze press with left mouse button on the button to the right of the maze, on the worksheet.
Change animation speed
You can change the speed of this by changing this line in the macro:
If (k / 100) - Int(k / 100) = 0 Then
If you want it really slow, change it to:
If (k / 1) - Int(k / 1) = 0 Then
If you want it faster, change it to:
If (k / 200) - Int(k / 200) = 0 Then
Count distance between start cell and end cell
Add this line:
MsgBox Tcount
before End Sub
VBA Code
'Name macro Sub BuildMaze() 'Disable screen refresh Application.ScreenUpdating = False 'Dimension variables and declare data types 'Array variable loc keeps track of the maze in creation Dim loc(0 To 110, 0 To 110) 'Array variable path keeps track of places to go not visited earlier Dim path(0 To 3) 'The visloc array variable keeps track of the current coordinate Dim visloc() As Variant 'Redimension array making it a dynamic array ReDim visloc(1, 0) 'Save value 1 to each cell in cell range B2:DG111 Range("B2:DG111") = 1 'Create a random value between 1 and 108 and save it ti variables StartR and StartC StartR = Int(Rnd * 108) + 1 StartC = Int(Rnd * 108) + 1 'Save text value S to a cell in cell range B2:DG111 based on random values stored in variables StartR and StartC Range("B2:DG111").Cells(StartR, StartC) = "S" 'Save number 1 to array variable loc based on random values stored in variables StartR and StartC loc(StartR, StartC) = 1 'Save random values stored in variables StartR and StartC to array variable visloc row 0 (zero), column 0 (zero) and row 1 and column 0 (zero) visloc(0, 0) = StartR visloc(1, 0) = StartC 'Do ... Loop statement until a condition is met meaning the lines between Do and Loop until will be rund repeatedly until the condition is met Do 'Save 0 (zero) to variable c c = 0 'For ... Next statement repeats line(s) between For and Next 4 times (0, 1, 2, 3) For i = 0 To 3 'Save zero to array variable path based on variable i path(i) = 0 Next i 'If ... then statement runs lines between If and End If if a condition is met If visloc(0, UBound(visloc, 2)) - 2 >= 1 Then 'If ... then statement runs lines between If and End If if the conditions are met If loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 2, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 Then 'Save number 1 to array variable path position 0 path(0) = 1 'Save number 1 to variable c c = 1 End If End If 'If ... then statement runs lines between If and End If if a condition is met If visloc(0, UBound(visloc, 2)) + 2 <= 110 Then 'If ... then statement runs lines between If and End If if the conditions are met If loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 2, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 Then 'Save number 1 to variable c c = 1 'Save number 1 to array variable path position 1 path(1) = 1 End If End If 'If ... then statement runs lines between If and End If if a condition is met If visloc(1, UBound(visloc, 2)) - 2 >= 1 Then 'If ... then statement runs lines between If and End If if the conditions are met If loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) - 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) - 2) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 Then 'Save number 1 to variable c c = 1 'Save number 1 to array variable path position 2 path(2) = 1 End If End If 'If ... then statement runs lines between If and End If if a condition is met If visloc(1, UBound(visloc, 2)) + 2 <= 110 Then 'If ... then statement runs lines between If and End If if the conditions are met If loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) + 2) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 Then 'Save number 1 to variable c c = 1 'Save number 1 to array variable path position 3 path(3) = 1 End If End If 'Check if variable c is equal to 0 (zero) If c = 0 Then 'Check if number saved in variable Ccount is larger than number stored in variable Tcount If Ccount > Tcount Then 'Save number stored in variable Ccount to variable Tcount Tcount = Ccount 'Save number stored in array variable visloc in position row 0 and column UBound(visloc, 2) Er = visloc(0, UBound(visloc, 2)) 'Save number stored in array variable visloc in position row 1 and column UBound(visloc, 2) Ec = visloc(1, UBound(visloc, 2)) End If 'Subtract number stored in variable Ccount with 1 Ccount = Ccount - 1 'Redimension array variable visloc ReDim Preserve visloc(UBound(visloc, 1), UBound(visloc, 2) - 1) 'Continue here if variable c is not equal to 0 (zero) Else 'Save 0 (zero) to variable c c = 0 'Keep iterating until c is not equal to 0 (zero) Do Until c <> 0 'Create a random integer between 0 and 4 rrand = Int(Rnd * 4) 'Check if number stored in array variable path position based on number in variable rrand is equal to 1 'If True then save number stored in variable rrand plus 1 to variable c If path(rrand) = 1 Then c = rrand + 1 Loop 'Redimension array variable visloc but keep previous values ReDim Preserve visloc(UBound(visloc, 1), UBound(visloc, 2) + 1) 'Select case statement. Based on what variable c contains case 1 to 4 is rund Select Case c 'If variable c is equal to 1 Case 1 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) - 1 visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) 'If variable c is equal to 2 Case 2 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) + 1 visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) 'If variable c is equal to 3 Case 3 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) - 1 'If variable c is equal to 4 Case 4 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) + 1 End Select 'Add 1 to number stored in variable Ccount Ccount = Ccount + 1 'Clear values in cell range B2:DG111 Range("B2:DG111").Cells(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2))) = "" 'Save number 1 to array variable visloc loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2))) = 1 'Apply changes DoEvents End If 'Add 1 to variable k k = k + 1 'If k is 50, 100, 150 and so on ... If (k / 50) - Int(k / 50) = 0 Then 'Show changes on screen Application.ScreenUpdating = True 'Hide changes on screen Application.ScreenUpdating = False End If 'Loop until array variable visloc is equal to start coordinates meaning the maze is complete Loop Until visloc(0, UBound(visloc, 2)) = StartR And visloc(1, UBound(visloc, 2)) = StartC 'Show changes on screen Application.ScreenUpdating = True 'Save text value B to cell in cell range B2:DG111 based on coordinates in variable Er and Ec Range("B2:DG111").Cells(Er, Ec) = "B" End Sub
Where to put the code?
- Press shortcut keys Alt + F11 to open the Visual Basic Editor (VB Editor).
- Press with left mouse button on "Insert" on the top menu, see image above.
- Press with mouse on "Module" to create a module in your workbook. The image above shows the module named Module1 in the Project Explorer window.
- Copy and paste VBA code to the module which is also shown in the image above.
- Exit the VB Editor and return to Excel.
2. Customize Excel maze
Terry wants to make a different sized maze and I think that is a great idea. Perhaps you remember that I built a maze in excel last week, if you don´t check it out.
This workbook allows you to choose the number of rows and columns you want, as long as it is between 1 and 255. You can also specify the column width and row height.
The following maze has 30 columns and 30 rows.
This maze has 200 columns and 200 rows and the column width is 3 px and row height is 3 px.
This maze has 60 columns and 40 rows.
VBA Code
'Name macro Sub BuildMaze() 'Dimension variables and declare data types 'Variable loc keeps track of location of the current cell Dim loc() As Variant 'Variable path keeps track of possible cells to go Dim path(0 To 3) 'visloc keeps track of visited cells, we don't want to move in a direction we already have been to. This keeps the macro from going into an endless loop Dim visloc() As Variant 'Redimension variable visloc ReDim visloc(1, 0) 'Disable screen refresh Application.ScreenUpdating = False 'Select worksheet Maze3 Worksheets("Maze3").Select 'Hide column and row headers ActiveWindow.DisplayHeadings = False 'Hide gridlines ActiveWindow.DisplayGridlines = False 'Select all cells Cells.Select 'Clear cell range A1:IZ260 Range(Cells(1, 1), Cells(260, 260)) = "" 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. With Worksheets("Customize maze") 'Redimension array variable loc using values in cell C7 and C4 ReDim loc(0 To .Range("C7").Value, 0 To .Range("C4").Value) 'Change selected cells columnwidths Selection.ColumnWidth = (.Range("C5").Value / 11.9047619) 'Change selected cells rowheights Selection.RowHeight = (.Range("C8").Value / (4 / 3)) 'Populate cell range with value 1 Range(Cells(2, 2), Cells((.Range("C7").Value + 1), (.Range("C4").Value) + 1)) = 1 'Create a random number based on value in cell C7 and save to variable StartR StartR = Int(Rnd * (.Range("C7").Value - 2)) + 1 'Create a random number based on value in cell C4 and save to variable StartC StartC = Int(Rnd * (.Range("C4").Value - 2)) + 1 End With 'Select cell A1 Range("A1").Select 'Enable screen refresh Application.ScreenUpdating = True 'Disable screen refresh Application.ScreenUpdating = False 'Save text value "S" to start cell based on variables StartR and StartC Range("B2").Cells(StartR, StartC) = "S" 'Save number 1 to array variable loc in position based on numbers in variables StartR and StartC loc(StartR, StartC) = 1 'Save number stored in variable StartR to array variable visloc position row number 0 (zero) and column number 0 (zero) visloc(0, 0) = StartR 'Save number stored in variable StartC to array variable visloc position row number 1 and column number 1 visloc(1, 0) = StartC 'Save value in cell C7 on worksheet "Customaize maze" to variable Ubr Ubr = Worksheets("Customize maze").Range("C7").Value 'Save value in cell C7 on worksheet "Customaize maze" to variable Ubr Ubc = Worksheets("Customize maze").Range("C4").Value 'Do ... Loop Until statement, repeat lines between Do and Loop until a condition is met Do 'Save 0 (zero) to variable c c = 0 'For ... Next statement, repeat line 4 times For i = 0 To 3 'Save number 0 (zero) to array variable path in a position based on variable i path(i) = 0 Next i 'These If Then statements check if the current cell is inside the boundaries 'If ... then statement If visloc(0, UBound(visloc, 2)) - 2 >= 1 Then If loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 2, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 Then 'Save 1 to variable path position 0 (zero) path(0) = 1 'Save 1 to variable c c = 1 End If End If 'If ... then statement If visloc(0, UBound(visloc, 2)) + 2 <= Ubr Then If loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 2, visloc(1, UBound(visloc, 2))) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 Then 'Save 1 to variable c c = 1 'Save 1 to variable path position 1 path(1) = 1 End If End If 'If ... then statement If visloc(1, UBound(visloc, 2)) - 2 >= 1 Then If loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) - 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) - 2) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) - 1) <> 1 Then 'Save 1 to variable c c = 1 'Save 1 to variable path position 2 path(2) = 1 End If End If 'If ... then statement If visloc(1, UBound(visloc, 2)) + 2 <= Ubc Then If loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2)) + 2) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) + 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 And _ loc(visloc(0, UBound(visloc, 2)) - 1, visloc(1, UBound(visloc, 2)) + 1) <> 1 Then 'Save 1 to variable c c = 1 'Save 1 to variable path position 3 path(3) = 1 End If End If 'Check if c equals 0 (zero) If c = 0 Then 'Check if Ccount is larger than Tcount If Ccount > Tcount Then 'Save value in variable Ccount to variable Tcount Tcount = Ccount 'Save values stored in array variable visloc to variables Er and Ec Er = visloc(0, UBound(visloc, 2)) Ec = visloc(1, UBound(visloc, 2)) End If 'Subtract 1 with number stored in variable Ccount and save it to Ccount Ccount = Ccount - 1 'Redimension array variable visloc ReDim Preserve visloc(UBound(visloc, 1), UBound(visloc, 2) - 1) 'Move here i c is not equal to 0 (zero) Else 'Save 0 (zero) to variable c c = 0 'Do Until ... Loop, iterate lines between Do and Loop until a condition is met Do Until c <> 0 'Save a random whole value between 0 and 3 rrand = Int(Rnd * 4) 'Check if array variable path in position based on value stored in variable rrand is equal to 1, if so, save value in variable rrand plus 1 to variable c If path(rrand) = 1 Then c = rrand + 1 Loop 'Redimension variable visloc, add a new container ReDim Preserve visloc(UBound(visloc, 1), UBound(visloc, 2) + 1) 'Select Case statement 'Lines being rund based on value in variable c Select Case c 'If variable c is equal to 1 then go here Case 1 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) - 1 visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) 'If variable c is equal to 2 then go here Case 2 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) + 1 visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) 'If variable c is equal to 3 then go here Case 3 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) - 1 'If variable c is equal to 4 then go here Case 4 visloc(0, UBound(visloc, 2)) = visloc(0, UBound(visloc, 2) - 1) visloc(1, UBound(visloc, 2)) = visloc(1, UBound(visloc, 2) - 1) + 1 End Select 'Save value in variable Ccount plus 1 to variable Ccount, in other words, add 1 to value in variable Ccount Ccount = Ccount + 1 'Clear cell range based on value in array variable visloc Range("B2").Cells(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2))) = "" 'Save number 1 to array variable loc in a position based on values in array variable visloc loc(visloc(0, UBound(visloc, 2)), visloc(1, UBound(visloc, 2))) = 1 'Show changes on screen 'Add another DoEvents to make this macro work in Excel 365 subscription DoEvents End If 'Add 1 to variable k k = k + 1 'Show changes on screen every 200 based on variable k If (k / 200) - Int(k / 200) = 0 Then Application.ScreenUpdating = True Application.ScreenUpdating = False End If Loop Until visloc(0, UBound(visloc, 2)) = StartR And visloc(1, UBound(visloc, 2)) = StartC 'Show changes on screen Application.ScreenUpdating = True 'Save text value B to end point based on values in variables Er and Ec Range("B2").Cells(Er, Ec) = "B" End Sub
3. Solve a maze programmatically in Excel
This article demonstrates a macro that finds a way between a start and an end point in a maze. My last article showed you how to build a random maze programmatically. You might remember that there was only one path between the start point and the end point. The macro moves randomly around the maze and remembers where it has been.
As soon as it finds the end coordinates, it stops. Conditional formatting highlights the found path yellow. You can sometimes see the macro going back and trying a new path. I have inserted two arrows to make it easier for you to find the start point and the end point. The animated image below shows the macro highlighting a path between the start and end point.
Show visited cells
You can change the macro to also show paths that it did search but couldn't find the end point. The image above shows that it has been almost all over the maze, in this particluar example.
It is also possible to highlight visited cells, change this line:
Range("B2:DG111").Cells(loc(0, UBound(loc, 2)), loc(1, UBound(loc, 2))) = ""
to
Range("B2:DG111").Cells(loc(0, UBound(loc, 2)), loc(1, UBound(loc, 2))) = "V"
Also create a new conditional formatting rule:
Apply the rule to cell range =$B$2:$DG$111. I picked a grey formatting color.
VBA Code
'Name macro Sub FindPath() 'Dimension variable and declare data type Dim loc() As Variant 'Redimension variable loc to make it an array variable ReDim loc(1, 0) 'Disable screen refresh Application.ScreenUpdating = False 'Search for a cell matching text value "S" and save it to an object named Sp (Start point) Set Sp = Cells.Find("S", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) 'Save the column number of the start cell to array variable loc position row 1 and column 0 loc(1, 0) = Sp.Column - 1 'Save the row number of the start cell to array variable loc position row 0 and column 0 loc(0, 0) = Sp.Row - 1 'Search for a cell matching text value "E" and save it to an object named Ep (End point) Set Ep = Cells.Find("E", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) 'Save the column number of the end cell to variable Ec Ec = Ep.Column - 1 'Save the row number of the end cell to variable Er Er = Ep.Row - 1 'Save values in cell range B2:DG111 to variable mtx mtx = Range("B2:DG111").Value 'Clear End point value in array variable mtx based on variables Er and Ec mtx(Er, Ec) = "" 'Save 0 (zero) to variable Counter Counter = 0 'Do ... Loop Until statement - Repeat executing lines between Loop and Loop Until until a given condition is met Do 'Save boolean value False to variable chk chk = False 'Check if a values in array variable mtx is equal to "" (nothing), if so change variable chk to boolean value True 'In other words check if a value next to the current location is equal to "" (nothing) 'Values next to the current location can be the adjacent value up, down, right or left. If mtx(loc(0, UBound(loc, 2)) + 1, loc(1, UBound(loc, 2))) = "" Then chk = True If mtx(loc(0, UBound(loc, 2)) - 1, loc(1, UBound(loc, 2))) = "" Then chk = True If mtx(loc(0, UBound(loc, 2)), loc(1, UBound(loc, 2)) + 1) = "" Then chk = True If mtx(loc(0, UBound(loc, 2)), loc(1, UBound(loc, 2)) - 1) = "" Then chk = True 'Check if variable chk is equal to boolean value False If chk = False Then 'Check if variable loc (current location) is equal to the starting point If loc(0, UBound(loc, 2)) = Sp.Row - 1 And loc(1, UBound(loc, 2)) = Sp.Column - 1 Then 'Continue here if variable loc (current location) is not equal to the starting point Else 'Save "" (nothing) to cell in cell range B2:DG111 based on values in array variable loc Range("B2:DG111").Cells(loc(0, UBound(loc, 2)), loc(1, UBound(loc, 2))) = "" 'Redimension array variable loc ReDim Preserve loc(UBound(loc, 1), UBound(loc, 2) - 1) 'Check if variable Tcounter is smaller than variable Counter If Tcounter < Counter Then 'Save value in variable Counter to variable Tcounter Tcounter = Counter ' Exr = loc(0, UBound(loc, 2) - 1) Exc = loc(1, UBound(loc, 2) - 1) End If 'Subtract variable Counter with 1 Counter = Counter - 1 End If 'Continue here if variable chk is not equal to boolean value False Else 'Redimension variable loc ReDim Preserve loc(UBound(loc, 1), UBound(loc, 2) + 1) 'Save boolean value False to variable c c = False 'Do ... Loop Until statement 'Repeat executing lines between Do and Loop until variable c equals boolean value True Do Until c = True 'Create a random whole number between 0 (zero) and 3 and save it to variable rrand rrand = Int(Rnd * 4) 'Select Case statement, allows you to control which lines to be rund based on the outcome of the random value Select Case rrand 'Go here if variable rrand is equal to 0 (zero) Case 0 'Check if cell value to the right of the current cell is equal to "" (nothing) If mtx(loc(0, UBound(loc, 2) - 1) + 1, loc(1, UBound(loc, 2) - 1)) = "" Then loc(0, UBound(loc, 2)) = loc(0, UBound(loc, 2) - 1) + 1 loc(1, UBound(loc, 2)) = loc(1, UBound(loc, 2) - 1) 'Save boolean value True to variable c c = True End If 'Go here if variable rrand is equal to 1 Case 1 'Check if cell value to the left of the current cell is equal to "" (nothing) If mtx(loc(0, UBound(loc, 2) - 1) - 1, loc(1, UBound(loc, 2) - 1)) = "" Then loc(0, UBound(loc, 2)) = loc(0, UBound(loc, 2) - 1) - 1 loc(1, UBound(loc, 2)) = loc(1, UBound(loc, 2) - 1) 'Save boolean value True to variable c c = True End If 'Go here if variable rrand is equal to 2 Case 2 'Check if cell value above the current cell is equal to "" (nothing) If mtx(loc(0, UBound(loc, 2) - 1), loc(1, UBound(loc, 2) - 1) + 1) = "" Then loc(0, UBound(loc, 2)) = loc(0, UBound(loc, 2) - 1) loc(1, UBound(loc, 2)) = loc(1, UBound(loc, 2) - 1) + 1 'Save boolean value True to variable c c = True End If 'Go here if variable rrand is equal to 3 Case 3 'Check if cell value below the current cell is equal to "" (nothing) If mtx(loc(0, UBound(loc, 2) - 1), loc(1, UBound(loc, 2) - 1) - 1) = "" Then loc(0, UBound(loc, 2)) = loc(0, UBound(loc, 2) - 1) loc(1, UBound(loc, 2)) = loc(1, UBound(loc, 2) - 1) - 1 'Save boolean value True to variable c c = True End If End Select Loop 'Save text value "S" to a value in array variable mtx mtx(loc(0, UBound(loc, 2)), loc(1, UBound(loc, 2))) = "S" 'Save text value "S" to a cell in cell range B2:DG111 Range("B2:DG111").Cells(loc(0, UBound(loc, 2)), loc(1, UBound(loc, 2))) = "S" 'Add 1 to variable Counter Counter = Counter + 1 End If 'Add 1 to variable k k = k + 1 'Show changes on screen every 500 steps based on variable k If (k / 500) - Int(k / 500) = 0 Then Application.ScreenUpdating = True Application.ScreenUpdating = False End If Loop Until loc(0, UBound(loc, 2)) = Er And loc(1, UBound(loc, 2)) = Ec 'Save text value "E" to end point Range("B2:DG111").Cells(Er, Ec) = "E" 'Show changes on screen Application.ScreenUpdating = True End Sub
Maze category
More than 1300 Excel formulasExcel categories
13 Responses to “Build a maze programmatically in Excel”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
So how do we combine our two projects? :)
https://optionexplicitvba.com/2013/09/21/excel-3d-maze-update/
Jordan Goldmeier,
Great looking project but I can´t open the file: maze-example.xlsm
What am I doing wrong?
Really? Let me take a look at that.
Very beautiful
Hi, This is very cool, but I wonder what controls the thickness of the maze. In other words how can I make the maze a bit wider so my elderly mother can trace it with her aging eyes.
Thanks and a very neat project.
Wassim
Wassim,
1. Go to "View" on the ribbon
2. Enable "Headings"
3. Select column B:DG on the sheet
4. Change the column width
This is cool.
I'm going to try to decipher this and make a different sized maze (maybe even variable) ... unless you think it would make a good project and blog post for you (hint :)).
Either way, thanks.
[…] Terry wants to make a different sized maze and I think that is a great idea. Perhaps you remember that I built a maze in excel last week, if you don´t check it out. […]
Hi Oscar ,
Is it possible to create the maze so that both the entry and the exit are on the sides of the maze ? At present the end of the maze is in the middle of the maze.
Narayan
[…] Build a maze […]
[…] Solve a maze […]
my excel just comes up with random cells filled with 1
Wow! That was AWESOME! I was wondering if there was a way to do this using just the borders in the cells, like a ''traditional'' maze. Thanks!