Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 31

Thread: Help with solving a maze in VBA Excel

  1. #1

    Help with solving a maze in VBA Excel

    So this is a pretty easy task but I do not VBA at all and can not seem it figure it out . It is basically writing a VBA sub procedure for a 15x15 maze that begins in c3 and ends in q17. Simple D = down, R = right, L = left, U = up I know it should start with Sub Path Finder () but do not know how to build after. If someone could explain it would be greatly appreciated.

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    There is not enough detail in your question, for example if this was a maze then what would the dead ends and walls consist of? What would happen if you hit a wall? Where is the end goal?
    Why does it have to use keys d r l u instead of the arrow pad?
    Seems like a pretty strange task for excel also, is this some kind of homework?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    So this is a pretty easy task
    Wrong! It's a very difficult task.

    First question: How does one solve a 'Blind Maze,' one where you can't see any part of the maze except where you are standing? Because VBA is blind in that sense.

    How does BA handle dead end Paths and wall ends?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Quote Originally Posted by georgiboy View Post
    There is not enough detail in your question, for example if this was a maze then what would the dead ends and walls consist of? What would happen if you hit a wall? Where is the end goal?
    Why does it have to use keys d r l u instead of the arrow pad?
    Seems like a pretty strange task for excel also, is this some kind of homework?
    The purpose is the minute you hit run it automatically solves the maze. You dont actually play the game. Each cell has either L R D U to indicate up down left rigjt and it should read that so it would not hit the wall.

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    What are the walls made of?
    Would it need to move through the maze visually or report its route with data?
    Excel will do this almost instantly so if you want to see a route then you would have to slow it down, is that what you want to do?

    I am viewing this that every second for example you would like it to move a space and display the move?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    Every box that it moves through before getting to the end of would either be a different color or a * in that box or something to show the path.

  7. #7
    Quote Originally Posted by georgiboy View Post
    What are the walls made of?
    Would it need to move through the maze visually or report its route with data?
    Excel will do this almost instantly so if you want to see a route then you would have to slow it down, is that what you want to do?

    I am viewing this that every second for example you would like it to move a space and display the move?
    Every box that it moves through before getting to the end of would either be a different color or a * in that box or something to show the path.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Each cell has either L R D U to indicate up down left rigjt and it should read that so it would not hit the wall.
    OK, it is not a MAZE, it is a PATH.
    Sub PATHer()
    Dim Cel As Range
      Set cel = Range("???") 'set starting cell here
      Do While Cel.address <> "$?$?"  'Insert end cell address here in $A$1 style
        Select Case Cel.Value
          Case "R": Set Cel = Cel.Offset(0, 1)
          Case "L": Set Cel = Cel.Offset(0, -1)
          Case "U": Set Cel = Cel.Offset(-1, 0)
          Case "D": Set Cel = Cel.Offset(1, 0)
        End Select
      Loop
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Ahh a path,

    Smart code SamT

    If you wanted to follow the path visually would this need a Cel.Activate line?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    If you wanted to follow the path visually would this need a Cel.Activate line?
    No. VBA operates directly on the declared Object

    Range("A1").Activate
    Set Cel =Range("B2")
    Cel. Value = "X"
    Cel.Interior.Color = vbRed
    'A1 is still "activated"
    ActiveCell.Value = "Z"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  11. #11
    Quote Originally Posted by SamT View Post
    OK, it is not a MAZE, it is a PATH.
    Sub PATHer()
    Dim Cel As Range
      Set cel = Range("???") 'set starting cell here
      Do While Cel.address <> "$?$?"  'Insert end cell address here in $A$1 style
        Select Case Cel.Value
          Case "R": Set Cel = Cel.Offset(0, 1)
          Case "L": Set Cel = Cel.Offset(0, -1)
          Case "U": Set Cel = Cel.Offset(-1, 0)
          Case "D": Set Cel = Cel.Offset(1, 0)
        End Select
      Loop
    End Sub


    Thank you so much. For some reason it won't run. How would one specify that it should show its path ..either by some color or symbol?

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    For some reason it won't run
    Probably because there is no such Range as Range("???"), nor such Range Address as "$?$?"

    either by some color or symbol?
    See Post #10
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    I set the Range (???) to my starting cell & $?$? to my ending cell but it still wont run.

  14. #14
    Quote Originally Posted by SamT View Post
    Probably because there is no such Range as Range("???"), nor such Range Address as "$?$?"

    See Post #10
    set the Range (???) to my starting cell (c3) & $?$? ($q$17)to my ending cell but it still wont run.

  15. #15
    Quote Originally Posted by SamT View Post
    Probably because there is no such Range as Range("???"), nor such Range Address as "$?$?"

    See Post #10
    20180303_101143.jpg0


    That is the maze

  16. #16
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("D4:K10")) Is Nothing Then Application.Goto Target.Offset((UCase(Target) = "U") - (UCase(Target) = "D"), (UCase(Target) = "L") - (UCase(Target) = "R"))
    End Sub

  17. #17
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    it still wont run.
    Maybe it needs a new battery

    Seriously, "It won't run" is not enough information to discover the problem.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  18. #18
    Quote Originally Posted by snb View Post
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("D4:K10")) Is Nothing Then Application.Goto Target.Offset((UCase(Target) = "U") - (UCase(Target) = "D"), (UCase(Target) = "L") - (UCase(Target) = "R"))
    End Sub
    Can you explain that

  19. #19
    Quote Originally Posted by SamT View Post
    Maybe it needs a new battery

    Seriously, "It won't run" is not enough information to discover the problem.
    Screenshot_20180303-103239.jpg

    This should be the end result once the code is run

  20. #20
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    The thing that is being missed here as far as i can see is:

    The code works perfectly but does not show the user visually what is happening, what we get is Cel holding the address of the goal when the code has run.

    I was thinking (post 9) that it would be nice if the user could see the path visually.

    Sub PATHer()
      Dim Cel As Range
      Set Cel = Range("C3") 'set starting cell here
      Do While Cel.Address <> "$Q$18"  'Insert end cell address here in $A$1 style
        Select Case Cel.Value
          Case "R": Set Cel = Cel.Offset(0, 1)
          Case "L": Set Cel = Cel.Offset(0, -1)
          Case "U": Set Cel = Cel.Offset(-1, 0)
          Case "D": Set Cel = Cel.Offset(1, 0)
        End Select
        Cel.Interior.Color = vbRed
      Loop
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •