Search worksheets and if not found msgbox

mc136355

New Member
Joined
Mar 20, 2018
Messages
36
Hi
I have the following code which will search all wksheets for a given name (or part of) and I would like to display message if wksheet does not exist.

However when performing the searching the msgbox will pop up each time to say it does not exist before it has searched all wksheets.

is there code that will display the message after all wksheets have been searched

thanks

my code:

For Each wsFind In Sheets
With wsFind
If .Name Like myArr & "*" Then
MsgBox wsFind.Name & " LIKE " & myArr
Find_Sheet = True
Exit For

Else

MsgBox "DOES NOT EXIST"


End If

End With
Next
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
use a boolean then to tell you what happened previously...

Code:
Dim a As Boolean 'a is declared and set to False
For ...
    If sheet.Name = "whatever" Then
        a = True 'you found it
    End If
Next

MsgBox Iif(a,"Sheet whatever was present", "Not Present")
 
Last edited:
Upvote 0
Maybe
Code:
For Each wsFind In Sheets
   With wsFind
      If .Name Like myarr & "*" Then
         MsgBox .Name & " LIKE " & myarr
         Find_Sheet = True
         Exit For
      End If
      If Not Find_Sheet Then MsgBox "DOES NOT EXIST"
   End With
Next
 
Upvote 0
Hi thanks for the quick reply.

However when my code search through the wksheets for example searches for cat and the first sheet is dog then the above code states cat does not exist when it does. I need to be able to search throughout sheets then make a decision after search

thanks again
 
Upvote 0
your code just needs to be formatted so you only call the message box once and after the loop (my code)... you can try Fluff's way too, ive actually never used the Like keyword but i assume his does the same, never even knew the Like keyword existed lol :eeek:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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