+ Reply to Thread
Results 1 to 4 of 4

loop through specific sheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2013
    Location
    LA Baby!!
    MS-Off Ver
    Excel 2007
    Posts
    1,598

    loop through specific sheets

    Hello. How do loop through specific sheets?

    I saw this, but I want to select something easily, like sheets 5 through 15, for example.

    Sub loopthrusheetarray()
    Dim Sh As Worksheet
    For Each Sh In Sheets(Array("Sheet2", "Sheet6", "Sheet10"))
    MsgBox Sh.Range("a1")
    Next

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,324

    Re: loop through specific sheets

    Some options:

    Option Explicit
    
    Sub sDisplaySheetNames()
    
    Dim i As Long
    Dim sh As Worksheet
    Const lFSNo As Long = 5
    Const lLSNo As Long = 15
    
    For i = lFSNo To lLSNo
        Debug.Print "1 " & Sheets("Sheet" & i).Name
    Next i
    
    For i = lFSNo To lLSNo
        Debug.Print "2 " & Sheets(i).Name
    Next i
    
    For Each sh In ThisWorkbook.Worksheets
        If Replace(sh.CodeName, "Sheet", "") >= lFSNo _
        And Replace(sh.CodeName, "Sheet", "") <= lLSNo _
        Then
            Debug.Print "3 " & sh.Name
        End If
    Next sh
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: loop through specific sheets

    Alternatively from Sheet5 to Sheet15,
    Sub test()
        Dim ws As Worksheet
        For Each ws In Sheets(["Sheet"&transpose(row(5:15))])
            Debug.Print ws.Name
        Next
    End Sub
    based on sheetname.

    or,
    Sub test2()
        Dim ws As Worksheet
        For Each ws In Sheets([transpose(row(5:15))])
            Debug.Print ws.Name
        Next
    End Sub
    based on sheets by index number 5 to 15.


    @TMS, I like your approach on using CodeName.
    Last edited by berlan; 08-17-2015 at 07:06 PM.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,324

    Re: loop through specific sheets

    @berlan: mutual appreciation ... I like your alternatives too

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change all loop to specific loop
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2015, 06:32 PM
  2. Loop a macro for specific sheets
    By dav1b in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-22-2014, 08:54 AM
  3. Programming to automatically send text to specific sheets and specific columns.
    By GlobalExcelller in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2014, 10:07 PM
  4. [SOLVED] Loop codes through multiple sheets (not all sheets in workbook)
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2014, 02:11 AM
  5. Loop for adding specific worksheets in specific workbooks
    By pankaj8219 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-13-2013, 10:22 AM
  6. [SOLVED] loop action through all sheets in workbook skipping certain sheets
    By sawa85sa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2013, 06:40 PM
  7. [SOLVED] Creating Loop that creates and saves specific sheets(tabs) to PDF
    By dash11 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-02-2012, 04:14 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1