Pearlyn Tan
asked on
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.
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
Hope you're able to help!
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
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
Having trouble with condition 1.Hope you're able to help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER