Find Sheet

gerald24

Board Regular
Joined
Apr 28, 2017
Messages
95
Hi Guys,

Could you please check what's missing? I want to find sheet even though part of its name only is being searched.

Code:
Sub FindWS()
    Dim strWSName As String
    
    strWSName = InputBox("Enter the sheet name to serach for")
    If strWSName = vbNullString Then
        Exit Sub
    End If
    
    If SheetExists(strWSName) Then
        Worksheets(strWSName).Activate
    Else
        'look if it at least contains part of the name
        Dim s As Worksheet
        For Each s In ThisWorkbook.Sheets
            If InStr(s.Name, strWSName) > 0 Then
                s.Activate
                Exit Sub
            End If
        Next s
        MsgBox "That sheet name does not exist!"
    End If
    
End Sub

Code:
Function SheetExists(strWSName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets(strWSName)
    If Not ws Is Nothing Then SheetExists = True
End Function

Thaaaaaaanks
 
Last edited:
Maybe use this instead
Code:
If InStr(1, s.Name, strWSName, vbTextCompare) > 0
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Not different from what you initially sent

Code:
Sub FindWS()
    Dim strWSName As String
    
    strWSName = InputBox("Enter the sheet name to serach for")
    If strWSName = vbNullString Then
        Exit Sub
    End If
    
    If SheetExists(strWSName) Then
        Worksheets(strWSName).Activate
    Else
        'look if it at least contains part of the name
        Dim s As Worksheet
        For Each s In ThisWorkbook.Sheets
            If InStr(1, s.Name, strWSName, vbTextCompare) > 0 Then
                s.Activate
                Exit Sub
            End If
        Next s
        MsgBox "That sheet name does not exist!"
    End If
    
End Sub
Function SheetExists(strWSName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets(strWSName)
    If Not ws Is Nothing Then SheetExists = True
End Function
 
Upvote 0
again I tried using your code. so in a new workbook there are sheet1, sheet2, and sheet 3

I tried searching for "2" or "3" but to no avail
I am using Excel 2010, does that have an effect?
Not different from what you initially sent

Code:
Sub FindWS()
    Dim strWSName As String
    
    strWSName = InputBox("Enter the sheet name to serach for")
    If strWSName = vbNullString Then
        Exit Sub
    End If
    
    If SheetExists(strWSName) Then
        Worksheets(strWSName).Activate
    Else
        'look if it at least contains part of the name
        Dim s As Worksheet
        For Each s In ThisWorkbook.Sheets
            If InStr(1, s.Name, strWSName, vbTextCompare) > 0 Then
                s.Activate
                Exit Sub
            End If
        Next s
        MsgBox "That sheet name does not exist!"
    End If
    
End Sub
Function SheetExists(strWSName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets(strWSName)
    If Not ws Is Nothing Then SheetExists = True
End Function
 
Upvote 0
Not different from what you initially sent

Code:
Sub FindWS()
    Dim strWSName As String
   
    strWSName = InputBox("Enter the sheet name to serach for")
    If strWSName = vbNullString Then
        Exit Sub
    End If
   
    If SheetExists(strWSName) Then
        Worksheets(strWSName).Activate
    Else
        'look if it at least contains part of the name
        Dim s As Worksheet
        For Each s In ThisWorkbook.Sheets
            If InStr(1, s.Name, strWSName, vbTextCompare) > 0 Then
                s.Activate
                Exit Sub
            End If
        Next s
        MsgBox "That sheet name does not exist!"
    End If
   
End Sub
Function SheetExists(strWSName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets(strWSName)
    If Not ws Is Nothing Then SheetExists = True
End Function
Hi Momentman / fellow members,

I'm new to this forum and the info here really helped me alot in my current VBA module. I'm reaching out because i'm caught in a bottleneck right now which i have no solution for.


Would you / anyone happen to know how i can exit sub if the sheet doesn't exist? I used the original code that was posted here but i changed the input portion and instead, indicated the string name i needed to search for.

I'm trying to do a scenario where if any of the worksheets contain the text "rawfile", another sub procedure will be called upon. Otherwise, if none of the worksheets contain the text "rawfile", i'll exit the Sub Activate_TempRawFile().

My current syntax exits the sub immediately and I'm not sure how should i write the Else statement for my desired scenario to happen.

Your help will be greatly appreciated! Thanks in advance!


-----------------------------------------------------------


Sub Activate_TempRawFile()

Dim strWSName As String
Dim ws As Worksheet
Dim s As Worksheet


strWSName = "rawfile"

MsgBox "The name of the active workbook is " & ActiveWorkbook.Name

For Each ws In ActiveWorkbook.worksheets
ws.Name = UCase(ws.Name)

Next


If SheetExists(strWSName) Then
worksheets(strWSName).Activate





Else
'look if it at least contains part of the name
For Each s In ThisWorkbook.Sheets
If InStr(1, s.Name, strWSName, vbTextCompare) > 0 Then
s.Activate
Exit For

Else: Exit Sub
MsgBox "Worksheet is not found. The macro will now end."

End If

Next s
MsgBox "That sheet name does not exist!"

End If


Call FormatDate_Column_A

End Sub


------------------------------------------------------------
Function SheetExists(strWSName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = worksheets(strWSName)
If Not ws Is Nothing Then SheetExists = True
End Function
 
Upvote 0
You shouldn't really be using the function SheetExists as it's designed to find a sheet with an exact, specific name.

Do you want to run the same code on every worksheet that contains 'rawfile' in the name?
 
Upvote 0
You shouldn't really be using the function SheetExists as it's designed to find a sheet with an exact, specific name.

Do you want to run the same code on every worksheet that contains 'rawfile' in the name?
Hi Norie,

Thanks for your tip and noted on the portion regarding SheetExists.

The objective of this code is to search within the active workbook to see if any of the worksheets contain the text "rawfile". If there's such a worksheet, another sub procedure will be called upon.

Otherwise, if none of the worksheets in the workbook contain the text "rawfile", i'll wish to exit the Sub Activate_TempRawFile().
 
Upvote 0
Could you start a new thread for your question?

Include a link back to this thread for reference.
 
Upvote 0

Forum statistics

Threads
1,224,766
Messages
6,180,846
Members
453,001
Latest member
coulombevin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top