If range contains specific text then - VBA

How do I create a condition that my worksheet contains a certain text?
I have workbooks that have multiple worksheets, of which some are relevant while some are not. I am trying to do some action on the worksheets that are relevant and the only similarity among them is that they contain "Product Name" in one of the cells. They have different sheet names and the "Product Name" in one worksheet might not be in the same cell as in another worksheet.
So far I've only found solutions for if a particular cell holds specific text, or non VBA solutions for what I'm looking for, but I'll need it to be part of my macro.

I have tried the following code, hoping that if at least one of the cells from A1:AA50 contains "Product Name", then my code will run. However I get an error saying there is a run-time error 13, type mismatch.

Sub testpn()
If InStr(1, ActiveSheet.Range("A1:AA50").Value, "Product Name") > 0 Then
    MsgBox "yes!" 'my code here if sheet contains "Product Name"
    Else: MsgBox "no!" 'my code here for an error message
End If

End Sub

Open in new window


For further context, not sure if it is a relevant limitation in your suggested code: I need to do multiple conditions, meaning:
Condition 1: if sheet contains "Product Name"
Condition 2: X and Y and Z

If condition 1 is true, then
  if condition 2 is true then
    run my code
    Else: don't run code, error message
  end if
  Else: no error message
End if

Open in new window

Having trouble with condition 1.

Hope you're able to help!
Pearlyn TanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
Sub testpn()
Set fnd = ActiveSheet.Range("A1:AA50").Find("Product Name")
If Not fnd Is Nothing Then
    MsgBox "yes!" 'my code here if sheet contains "Product Name"
    Else: MsgBox "no!" 'my code here for an error message
End If

End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pearlyn TanAuthor Commented:
Works perfectly. Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.