+ Reply to Thread
Results 1 to 8 of 8

If Worksheet like (*Comments*) exists do the following. Use of wildcards in Function

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Question If Worksheet like (*Comments*) exists do the following. Use of wildcards in Function

    Hi All.

    I have a very useful function that I have found online:
    Function wsExists(wksName As String) As Boolean
        On Error Resume Next
        wsExists = CBool(Len(Worksheets(wksName).Name) > 0)
        On Error GoTo 0
    End Function
    
    'Example
    Sub testUDF()
        If wsExists("Comments") Then
            MsgBox "It exists"
    Else: MsgBox "Not there"
        End If
    End Sub
    The above function works great - What it doesn't do is allow the use of a wildcard in the sheet name to search for.

    What I would like is a modification to the above function or code to allow me to use a wildcard to search for sheets
    ...Like (*Comments*) then ...
    Any help appreciated as always.
    Cheers,
    Mark
    Last edited by R_S_6; 10-14-2010 at 05:04 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: If Worksheet like (*Comments*) exists do the following. Use of wildcards in Funct

    This is one way with a different approach:

    Function wsPartExist(wksName As String) As Boolean
    
    Dim wsEachSheet
    
    For Each wsEachSheet In Worksheets
    
        If InStr(UCase(wsEachSheet.Name), UCase(wksName)) > 0 Then wsPartExist = True
    
    Next wsEachSheet
    
    End Function

    Dom
    Last edited by Domski; 10-07-2010 at 09:10 AM.
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,332

    Re: If Worksheet like (*Comments*) exists do the following. Use of wildcards in Funct

    You could use something like:
    Function wsExists(ByVal wksName As String, Optional blnPartial As Boolean = False) As Boolean
        Dim wks As Worksheet
        If blnPartial Then
            For Each wks In ActiveWorkbook.Worksheets
                If InStr(1, wks.Name, wksName, vbTextCompare) > 0 Then
                    wsExists = True
                    Exit Function
                End If
            Next wks
        Else
            On Error Resume Next
            wsExists = CBool(Len(Worksheets(wksName).Name) > 0)
        End If
    End Function
    called as either:
        If wsExists("Comments") Then
    for exact match or:
        If wsExists("Comments", true) Then
    for partial match.

    If you want specific combinations of wildcards, then you could use Like instead.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Question Re: If Worksheet like (*Comments*) exists do the following. Use of wildcards in Funct

    Thank you both for your help.

    I have utilised Dom's solution as I had to get it going ASAP.

    I am interested romperstoper how you would utilise the 'Like' coding in your example for combos of wildcards???

    Lastly i'm using this code in the following:
    Sub Workbook_BeforeClose(Cancel As Boolean)
    
        If wsPartExist("Comments") Then
                'MsgBox "It exists"
                Cancel = False
                
        Else
        MsgBox "Not there"
        Application.Run "'" & ActiveWorkbook.Name & "'" & "!Show_Comments.Show_Comments"
        Cancel = False
        'Cancel = True
        
        End If
        
    End Sub
    'Cancel = False' allows the workbook to close. Is there a way I can add to this line to allow it to save without the user input of 'Yes' 'No' 'Cancel'?? a bit like how you would use:
    activeworkbook.close True
    Thanks in advance!
    Mark

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: If Worksheet like (*Comments*) exists do the following. Use of wildcards in Funct

    Application.DisplayAlerts = False
    ActiveWorkbook.Close True
    Application.DisplayAlerts = True

    Dom

  6. #6
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Talking Re: If Worksheet like (*Comments*) exists do the following. Use of wildcards in Funct

    Cheers Dom,

    Nice One!

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,332

    Re: If Worksheet like (*Comments*) exists do the following. Use of wildcards in Funct

    Untested, but something like:
    Function wsExists(ByVal wksName As String, Optional blnPartial As Boolean = False, Optional blnMatchCase As Boolean = False) As Boolean
        Dim wks As Worksheet
        If blnPartial Then
            wksName = Replace(wksName, "*", "%")
            If blnMatchCase Then
                For Each wks In ActiveWorkbook.Worksheets
                    If wks.Name Like wksName Then
                        wsExists = True
                        Exit Function
                    End If
                Next wks
            Else
                wksName = UCase$(wksName)
                For Each wks In ActiveWorkbook.Worksheets
                    If UCase$(wks.Name) Like wksName Then
                        wsExists = True
                        Exit Function
                    End If
                Next wks
            End If
        Else
            If blnMatchCase Then
                For Each wks In ActiveWorkbook.Worksheets
                    If StrComp(wks.Name, wksName, vbBinaryCompare) = 0 Then
                        wsExists = True
                        Exit Function
                    End If
                Next wks
            Else
                On Error Resume Next
                wsExists = CBool(Len(Worksheets(wksName).Name) > 0)
            End If
        End If
    End Function

  8. #8
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Re: If Worksheet like (*Comments*) exists do the following. Use of wildcards in Funct

    Thanks for that Romperstomper - I'll give it a go ;-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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