help for macro looping in excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monmon
    New Member
    • Feb 2013
    • 3

    help for macro looping in excel

    i have this code that needs to be loop nth time..

    Code:
    Sub Macro12()
    '
    ' Macro12 Macro
    '
    
    '
        Range("A2:D6").Select
        Application.CutCopyMode = False
        Selection.Copy
        Range("E2").Select
        ActiveSheet.Paste
        Range("A7:D11").Select
        Application.CutCopyMode = False
        Selection.Copy
        Range("E7").Select
        ActiveSheet.Paste
        Range("E15").Select
    End Sub
    im a newbie in VB so dont know how to change the value of A2:D6 to increment to 5.. so it will be A7:D11 for the next loop. as well as where to paste E2 to E7.
    Last edited by Niheel; Feb 28 '13, 03:33 AM.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It looks like you already have the two ranges in your code. Although it doesn't make sense why you would split it when you can just do it in one go by using A2:D11.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      do a search on relative cell address in VBA by opening the VBA editor to your code and press [F1] there is was a nice example in the [F1] files that will show you step by step.
      Let us know if that isn't available to you.

      Comment

      • monmon
        New Member
        • Feb 2013
        • 3

        #4
        its just actually copy A2:A6 then paste to B2. then copy A7:A11 then copy to B7. I just posted the two ranges to let the helper know the range from which the next loop would be.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          That's fine,
          You still need to use the relative cell address with offset for movement.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            What I'm trying to say is that copying A2:A6 and then copying A7:A11 is the same thing as just copying A2:A11.

            Comment

            • monmon
              New Member
              • Feb 2013
              • 3

              #7
              nope.. because in the first range.. you have to paste it in b2.. second in b7

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                It's the same thing. Copying A2:A11 and pasting it into B2 is the exact same result as copying A2:A6, pasting into B2, then copying A7:A11, and pasting into B7.

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  monmon:
                  Look, what we're getting at is that:
                  What you are doing within the code that you've already posted simply needs to be repeated.
                  I've told you to look-up "relative cell addressing" and the "Offset" method.
                  There is also in Excel VBA a direct method of selecting and pasting selected cells.
                  All of these methods are easily found by opening your VBA-Editor and searching help.
                  What you will not get here is a complete code .

                  Comment

                  Working...