Consulting

Results 1 to 5 of 5

Thread: Solved: Count Sheets With Specific Name

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Solved: Count Sheets With Specific Name

    Anyone know how to count the number of worksheets that start with "Playoff". I have multiple sheets named "Playoff 1", Playoff 2", etc. The number of Playoff sheets vary from season to season so I am trying to count the number of sheets that begin with Playoff.

    Thanks
    Gary

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Gary,

    You can loop through the sheets, like:
    Sub zoom38CountWSNames()
     Dim WS As Worksheet, iCnt As Long
     For Each WS In ActiveWorkbook.Worksheets
      If InStr(1, WS.Name, "Playoff", vbTextCompare) = 1 Then iCnt = iCnt + 1
     Next
     MsgBox "There are " & CStr(iCnt) & " sheets that start with 'playoff'"
    End Sub
    Matt

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Very nice MV but I do have a problem. I have sheets named "Playoff 1", "Playoff 2" etc. but I also have a sheet named "Playoff Stats" that is also being counted. Is there a way to just count the other sheets outside of the obvious by subtracting 1?

    Thank
    Gary

  4. #4
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    So you want it to be Playoff followed by a number? Sure, no problem, though this time I'm going to use Like instead of InStr:
    Sub zoom38CountWSNames()
     Dim WS As Worksheet, iCnt As Long
     For Each WS In ActiveWorkbook.Worksheets
      If LCase(WS.Name) Like "playoff #*" Then iCnt = iCnt + 1
     Next
     MsgBox "There are " & CStr(iCnt) & " sheets that start with 'playoff'"
    End Sub
    Matt

  5. #5
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Outstanding MV works perfectly.

    Thank You
    Gary

Posting Permissions

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