Using Instr to compare strings and numbers

Reset

Board Regular
Joined
Apr 16, 2010
Messages
227
I have two lists and want to compare to find matches, but in one they are strings and the other they are values. I tried to load the numebrs into a string array but got "invalid qualifier". Here's the code:

Sub t()
Workbooks("Book1.xlsx").Activate
a = Application.WorksheetFunction.CountA(Columns(1))
ReDim d1(a) As String
For x = 1 To a
d1(x) = Cells(x, 2).Value
Next x
Workbooks("Book2.xls").Activate
b = Application.WorksheetFunction.CountA(Columns(1))
ReDim d2(b) As String
For c = 2 To b
d2(c) = Cells(c, 1)
Next c
For x = 1 To a
For y = 2 To b
If InStr(1, d2(y).Text, d1(x), 1) > 0 Then Rows(y).Interior.ColorIndex = 8
Next y
Next x
End Sub

Anyone know how I can compare? I don't want to convert the numbers stored as text in the worksheet. They are text in Book1 and numbers in Book2. Thanks.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Why don't you use the Cstr function in your code just for the comparisons?

That needn't cause any change in the numbers in your worksheet.
 
Upvote 0
Because I am a noob and make things work through monotonous trial and error. If use the Excel help for Cstr it doesn't help me figure out how I would use it.
 
Upvote 0
When invalid qualifier comes up it highlights the "d2" in the "d2(y).Text" portion in the Instr line.
 
Upvote 0
Then perhaps try
Rich (BB code):
If InStr(1, Cstr(d2(y)), d1(x), 1) > 0 Then Rows(y).Interior.ColorIndex = 8
although without knowing more about your data setup that's really just a guess.

I note you already have a line
Rich (BB code):
ReDim d2(b) As String
so maybe the .Text bit is causing the trouble and just leaving that out might make the difference.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

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