Problem comparing a string to a list in VBA

roscoe

Well-known Member
Joined
Jun 4, 2002
Messages
1,062
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Background:
I read in data from a text file line by line ("line_info"). I then extract from that string the first characters up to the first space ("SSC"). The length of SSC is variable, and sometimes SSC is just numbers, and sometimes numbers+text (but always treated in the code as text). I then look to see if that string is included in a list (named range "Filter_list") in the workbook, then I keep the line, otherwise I skip it. The named range has values that are formatted as text but some are in reality straight numbers

Technique:
I use the following VBA code:
Code:
dim Keep as Boolean
dim SSC as string

SSC = Trim(Left(line_info,8)) '// pulls in first 8 characters then removes any excess spaces)
Keep = IsNumeric(WorksheetFunction.Match(SSC, Range("filter_list"))
Problem:Works if SSC is pure numbers ("12345") but if SSC has characters ("12345-A") the line fails.

How do I fix this? Is there a better way to compare a string to a list of numbers + text?

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you want an exact match to SSC then add a zero to the match function:

Also, try using:
Keep = Not Iserror(WorksheetFunction.Match(SSC,Range("filter_list"),0))
 
Upvote 0
I tried various versions and no matter what it failed. So I just added an "on error resume next" in front of it and it works fine.

Not sure why it isn't working but...
 
Upvote 0
Try

Code:
Keep = IsNumeric(Application.Match(SSC, Range("filter_list"))
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,310
Members
452,906
Latest member
phanmemchatdakenhupviral

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