Consulting

Results 1 to 6 of 6

Thread: Look For Sheets Wildcard no Loop

  1. #1

    Look For Sheets Wildcard no Loop

    Hello,

    I am wondering if there is a way to find a specific group of sheets with a wildcard without looping. I have this code and it works perfectly. I am just curious to see if there is another way. I think performance will improve when the code has to go through 300 sheets.

    In my mind I was thinking on a way similar to how DIR() behaves.

    Sub Try() '
    
    Dim sh As Worksheet
    
    
    Const strNAME As String = "AP-*"
    
    
    For Each sh In ActiveWorkbook.Sheets
        If sh.Name Like strNAME And sh.Visible = xlSheetHidden Then
            sh.Visible = xlSheetVisible
        End If
    Next sh
        
    End Sub
    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    But you would still have to loop to test the visibility.

    As long as the workbook name doesn't include your search text, you could use:
        Dim vSheets    
        ActiveWorkbook.Names.Add "SheetList", "=GET.WORKBOOK(1)"
        vSheets = Filter(ActiveSheet.Evaluate("SheetList"), "AP-")
    but I don't really see the point.
    Last edited by Bob Phillips; 06-25-2014 at 08:41 AM.
    Be as you wish to seem

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    From what I've read, Like is a slow operator, so a few minor suggestions.

    I'd just make them visible all the time

    Sub Try2()
         
        Dim sh As Worksheet 
         
        For Each sh In ActiveWorkbook.Sheets
            If Left(sh.Name,3) = "AP-" then sh.Visible = xlSheetVisible 
        Next sh 
         
    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

  4. #4
    Quote Originally Posted by Paul_Hossler View Post
    From what I've read, Like is a slow operator
    Wow I did not know that. Thanks a lot for the information Paul. The only problem about this is that I will have to pay attention to the number of chars.

    I wonder how much of a difference "like" compared to "Left$()"

    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    http://www.aivosto.com/vbtips/stringopt.html

    http://www.aivosto.com/vbtips/stringopt2.html

    http://www.aivosto.com/vbtips/stringopt3.html

    Like

    The Like operator is not particularly fast. Consider alternatives. We don't have a generic rule to follow here. You need to measure the performance differences between your alternatives. Here is one rule though. It applies if you're looking for a certain string inside another one.
    Instead of:
    If Text$ Like "*abc*" Then

    Use:
    If InStr(Text$, "abc") <> 0 ThenYou may also use InStrB if you know what you're doing
    But it most likely will not much difference for 300 or so compares
    ---------------------------------------------------------------------------------------------------------------------

    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

  6. #6
    Thanks

    I was just reading the same articles. Yes, I am not going back and change all my "like" now but its something to keep in mind for the future or when I get back to the code to refactor.

    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

Posting Permissions

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