PDA

View Full Version : VBA to Search More than One Worksheet in a Workbook



infinity
01-30-2022, 10:40 AM
Hello all you awesome Vbaxers,

It has been quite a while since I have posted a question and that is because I have learned so much from all of you (as well as other forums) that I am fairly self-sufficient and a lot of times I can figure out what to do, even though I'm not a pro by any means.

Here is one that I have tried to figure out on my own, I have searched vbaexpress and other places on the web to find an answer I have been unsuccessful so far, maybe I am just wording my search incorrectly. Can anyone help?

I want VBA Code that will search for a value that is on a UserForm (which I have already created). If it finds the value on the current sheet, it runs code (already written and working). If it doesn't find it on that sheet, it goes to a second sheet in the same workbook and searches for it there. If it finds it there, it runs a different code (also already written and working). If it does not find it on the second sheet, it gives me a MsgBox telling me the value is not found, which when "OK" is pressed on the MsgBox it will Exit Sub.

Hopefully this makes sense.

jolivanes
01-30-2022, 11:09 PM
Re: "a value that is on a UserForm"
Label? TextBox? ComboBox? or whatever.

jolivanes
01-30-2022, 11:54 PM
Sub Maybe()
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
If WorksheetFunction.CountIf(sh1.UsedRange, "Hallo") <> 0 Then
Macro1
Exit Sub
ElseIf WorksheetFunction.CountIf(sh2.UsedRange, "Hallo") <> 0 Then
Macro2
Exit Sub
End If
MsgBox "No such value"
End Sub