Using a counter within a name in VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertGS
    New Member
    • Sep 2013
    • 2

    Using a counter within a name in VBA

    I am trying to automate a simple function that resets shapes to a specified location.

    For instance, let's say there are 4 blue squares on your worksheet, titled Blue1-Blue4.

    The following lines of code accomplish what I wish to do:


    Code:
    ActiveSheet.Shapes("blue4").Left = 20
    ActiveSheet.Shapes("blue4").Top = 40


    But this requires 4 repititions of those two lines of code. I am looking for a loop that will allow me to make this happen to all the squares at once, as I have a lot more than 4 blue squares. Something along the lines of:


    Code:
    For i = 1 To 4
    ActiveSheet.Shapes("Blue" & i).Left = 20
    ActiveSheet.Shapes("Blue" & i).Top = 40
    
    Next i
    Additional information:
    Yes I want the shapes to all go to the same place.

    Thanks in advance to anyone who can help.

    -Rob
    Last edited by Rabbit; Sep 2 '13, 04:46 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    Your thread has been moved to the Excel forum.

    You can do exactly what you have in your post.

    Comment

    • RobertGS
      New Member
      • Sep 2013
      • 2

      #3
      Thank you for editing and moving my post as appreciated. I posted my question before I joined, so I didn't realize I hadn't followed all the requirements. For that, apologies.

      In response to your answer, I thought I had tried exactly that. I went back and tried it again, and it worked. I'm not really sure what I did differently before that did not work.

      Thank you so much for your reply, I greatly appreciate it.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        No problem, it was most likely just a typo last time it didn't work, happens to everyone.

        Comment

        Working...