[Solved] VBA: Help with a loop...

  • In a range from D2:D50 there will be page numbers. They can be random (i.e. - 2, 3, 5, 10, 11, 12, 15, etc...). What I am trying to do is run a loop script on this column of numbers, once per number, until it reaches the end. Can someone please fill in the blanks for me on this one?


    Do
    Sheets("All Data").Select
    Range("D2").Select
    Selection.Copy
    Sheets("All Trip Sheets").Select
    Range("H2:I3").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    Loop


    I don't know how to do "Do..Loops". The basic function I'm trying to accomplish is to copy and paste the page number from "All Data" into the "All Trip Sheets" page, into merged cell H2:I3. It will then print that page and move to the next number in the list.


    Any ideas?

  • Assuming I understand what you're trying to do there are several ways. You can use a For..Next loop instead of Do..Loop with minimal change to your current code:


    For i = 2 To 50
    Sheets("All Data").Select
    Range("D" & i).Select
    Selection.Copy
    Sheets("All Trip Sheets").Select
    Range("H2:I3").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    Next

  • Something like the foloowing should get you going. Incidently, you do not need to select a cell in order to write to it. It's faster just to assign it a value.



Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!