Finding Text With in Text

prajul89

Active Member
Joined
Jul 9, 2011
Messages
404
Is there a way to find out whether 7 is included in 1237654 or not in VBA without using Error handler?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Code:
If WorksheetFunction.IsNumber(WorksheetFunction.Find(7, 12345)) = True Then Exit Sub

Can you tell me Why doesn't this work??
 
Upvote 0
Code:
If WorksheetFunction.IsNumber(WorksheetFunction.Find(7, 12345)) = True Then Exit Sub

Can you tell me Why doesn't this work??
I'm not much of a programmer so someone else will need to help you with this.

You may be better off using a native VBA test instead of using VBA to evaluate a worksheet formula.
 
Upvote 0
You can test this in VBA as it is by putting Square Brackets around like:
Code:
If [IsNumber(Find(7, 12345))] = True Then Exit Sub

Or as Biff has suggested use VBA's built in InStr function can do this efficiently like:
Code:
If InStr(12345, 7) > 0 = True Then Exit Sub
 
Upvote 0
And if you want to implement your own code then you will have to add error handling.

Your arrangement will work fine when there's 7 in the other string.

But if it is not there then VBA raises an error. So revised code will look like this:
Code:
'Trapping the error that VBA will raise on not finding 7
On Error GoTo ContinueCode
If WorksheetFunction.IsNumber(WorksheetFunction.Find(7, 12345)) = True Then Exit Sub
ContinueCode:
'Resetting the error handling settings
On Error GoTo 0
MsgBox "Try this!"
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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