Consulting

Results 1 to 7 of 7

Thread: need help with counting sheets with specific names plus adding sheets after them

  1. #1

    need help with counting sheets with specific names plus adding sheets after them

    hi I would need help with a comment to count the sheets with name starting with "wf" in a workbook and then add a growing sheet after last tabs with "wf". I have other sheets with other names within that workbook so the new growing sheet needs to come exactly after the last found one with "wf" used.
    thank you so much
    Agata

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Something like
    Dim lastWFWorksheet as Worksheet
    Dim countOfWF as Long
    Dim oneSheet as Worksheet
    
    For each oneSheet in ThisWorkbook.Sheets
        If oneSheet.Name Like "wf*" Then
            countOfWF = countOfWF + 1
            Set lastWFWorksheet = oneSheet
        End If
    Next oneSheet
    ' ...
    MsgBox CStr(countOfWF) & " sheets names begin with wf (case sensitive)."
    ' ...
    If countOfWF <> 0 Then
        ThisWorkbook.Sheets.Add after:=lastWFWorksheet
    End If

  3. #3
    Hi
    thank you for help, however it gives me 0 count but the files starting with wf are there...

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Welcome to the forum!

    Mike's code worked fine for me. Did you notice that the search was case sensitive? In other words, wf5<>WF5.

    You lost me with files starting with. You asked for sheets with names starting with "wf".

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Are we talking worksheets, contained in the same workbook as the code.
    Or are we talking about files, in a folder?

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Mikes code: Case Insensitive
        If LCase(oneSheet.Name) Like "wf*" Then 
    '...
    MsgBox CStr(countOfWF) & " sheets names begin with wf (case insensitive)."
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Hallo,
    thank you all for help! I used following code:
    Dim WS As Worksheet, iCnt As Long
    Dim WW As Worksheet
    For Each WS In ActiveWorkbook.Worksheets
    Debug.Print WS.Name
    If InStr(1, Left(WS.Name, 2), "wf", vbTextCompare) > 0 Then 
                    myTotal = myTotal + 1
    different version was 'If InStr(1, WS.Name, "wf", vbTextCompare) = 1 Then iCnt = iCnt + 1 - also working
    'Set WW = WS
    End If
    Last edited by SamT; 01-20-2014 at 09:31 AM.

Posting Permissions

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