Consulting

Results 1 to 18 of 18

Thread: Solved: Need help with a simple loop.

  1. #1

    Solved: Need help with a simple loop.

    I need some help with what would seem like a simple simple loop. What I would like to do is start on sheet 3 do some stuff, go to next sheet, do same stuff, go to next sheet... ect till there are no more. I cannot seem to end it correctly.
    Here is what I have:

    Sheets("Sheet3").Select
    Do
    ActiveSheet.Next.Select
    'do some stuff
    Loop Until ??????
    End Sub

    Btw, is there a reference sheet on the web that can help with what is ok after loop until?

  2. #2
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    The Excel help files do a good job of explaining Do...Loop statements. If you're using 2007, make sure you are searching Developer Reference. Does ActiveSheet.Next.Select work?

  3. #3
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    you can try to put something like that into your Sub:

    [VBA]
    Dim i as Integer

    For i = 1 to 10
    Sheets(i).Select
    'Do some stuff
    Next i
    [/VBA]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    For i = 3 To Worksheets.Count

    With Worksheets(i)

    'do some stuff
    End With
    Next i
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks for the replies. I still am having some trouble though. I a little bit of a newb, so go easy on me.

    Yes, this part does work, ActiveSheet.Next.Select. it is just the until part I end up with an error on.

    Let me explain a little more to see if that helps.

    I have at least three sheets but don't know how many all together, it will vary.
    To keep things simple. lets say I want to clear (A1) starting on sheet 3
    and repeating through all the sheets that follow. Till the end.

    Thanks again

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Don't do it that way, we have shown you better ways.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    If you want to understand the basics of loops you can take a look at the article I wrote- just click on the Articles link under my name.
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  8. #8
    Quote Originally Posted by xld
    [vba]

    For i = 3 To Worksheets.Count

    With Worksheets(i)

    'do some stuff
    End With
    Next i
    [/vba]

    Ahhhhhhh, I finally got it. I kept running into a bug and missed the fact I was missing .select on line 2.
    In other words it should be

    For i = 3 To Worksheets.Count

    With Worksheets(i).select

    'do some stuff
    End With
    Next i


    Btw, I love this forum, I have a feeling I'm going to learn a lot here!

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No it shouldn't! Selecting is inefficient. Work via the referenced worksheet object
    , not a selected worksheet.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    i.e.
    [VBA]For i = 3 To Worksheets.Count
    With Worksheets(i)
    .Range("A1").ClearContents
    End With
    Next i [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Edit: I posted just after you md. I bet that is what I'm missing.

  12. #12
    That is it! Thank you everyone!

  13. #13
    I know I marked this as solved , but I wanted to see if you guys could help me with one more little thing I need to finish this macro up.

    If I wanted to have cell (a5) selected on these sheets when finished what would I need to write in?

    For i = 3 To Worksheets.Count
    With Worksheets(i)
    .Range("A1").ClearContents
    Activate (A5) Here on these sheets
    End With
    Next i

    I'm having trouble learning this type of code! Is this VBScript and where can I go to learn this?

  14. #14
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You have to reference the range just as you did with A1.

    [VBA].range("A5").select[/VBA]

    activate is the wrong action.....

    Also as Maxim shows in post #3 you should define the variabe i

    If you used Option explicit at the top of your module as you should always do, you would have recieved an error if you did not do this....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #15
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Actually select will not work either.....what are you doing with A5?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  16. #16
    I want to exit the macro with those cells activated if possible. In other words that would be the finishing touch.

  17. #17
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I believe the only way you can do that is to select each sheet which is not the most efficient way to go.
    [VBA]
    Option Explicit
    Sub a()
    Dim i As Long
    For i = 3 To Worksheets.Count
    Sheets(i).Select
    With ActiveSheet
    .Range("A1").ClearContents
    .Range("A5").Select
    End With
    Next i
    End Sub

    [/VBA]

    Unless it is absolutely necessary I would avoid this step and stick with the code provided above.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If it is appropriate, you can set Excel to go to a specific range when a sheet is activated. This code goes into the WorkBook module.
    [VBA]Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Select Case Sh.Name
    Case "Sheet1"
    Range("A5").Select

    Case "Sheet2"
    Range("B5").Select

    End Select
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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