Consulting

Results 1 to 5 of 5

Thread: Execute submacro a set number of times until criteria is met

  1. #1
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    402
    Location

    Execute submacro a set number of times until criteria is met

    I was wondering if there was a way to execute a sub on each worksheet inside the same workbook, whose worksheets were consecutively named, until the final worksheet was reached, at which point the sub would exit.

    I was thinking something like the following, although I could be way off!

    Option Explicit
    
    Sub TestSub()
    
        Dim ws     As Worksheet
        Dim i      As Integer
    
    
        For i = 1 To 6
            Do Until i = 6
                ' Total of 6 consecutive worksheets to work on, TestData1 through TestData6
                Application.ScreenUpdating = False
                
                ThisWorkbook.ws("TestData").Name (i)
                
                
                
                ' ..... Other bits of code here
                
                
                i = i + 1
            Loop
            Application.ScreenUpdating = True
        Next i
    End Sub

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Something like this?

    Sub TestSub()
    Dim ws     As Worksheet
    Dim i      As Long
            
        Application.ScreenUpdating = False
    
        With ThisWorkbook
    	
            For Each ws in .Worksheets
    
                ' ..... Other bits of code here
    	Next ws
        End With
    
        Application.ScreenUpdating = True
    End Sub
    ____________________________________________
    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

  3. #3
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    402
    Location
    Thanks, Bob!

    I can see that this will work its way through worksheets, but cannot see how this will increment them until a fixed number is arrived at?

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by HTSCF Fareha View Post
    Thanks, Bob!

    I can see that this will work its way through worksheets, but cannot see how this will increment them until a fixed number is arrived at?
    The

    For Each ws In .Worksheets
    will run on all worksheets in ThisWorkbook in the order that they are stored in Excel, which might not be TestData1, TestData2, ...



    If you have more than the ones that you want, you'll need something like

    Option Explicit
    
    
    Sub Demo()
        Dim ws As Worksheet
        
        For Each ws In ThisWorkbook.Worksheets
            If ws.Visible = xlSheetVisible Then
                If Left(ws.Name, 4) = "Test" Then
                    MsgBox ws.Name
                End If
            End If
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    402
    Location
    Paul, thanks for explaining this for me. Not forgetting Bob for his original answer!

Posting Permissions

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