VBA InStr Question

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,661
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this line of code that is meant to determine where in a string (value at ws_ifm.cells(trow,2)), a substring (host) starts. Everything works except when the value in the cell equals the value of host.
eg - ws_ifm.cells(trow,2).value = "Cheese", and host = "Cheese"

The code returns a value of 0 which would suggest the sub string doesn't exist in the main string, which in reality, the substring is the main string.

Is there a way to change this code to capture this (I would think the formula would return a value of 1). Or should I just test if the values are equal first?

VBA Code:
startPosition = InStr(1, ws_ifm.Cells(trow, 2), host, vbTextCompare)
     If startPosition = 0 Then
     '... execute code recognizing the value of host does not exist in the cell value
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Put this before startPosition line and step through the code. Make sure it prints out two values that are exactly the same.
debug.print ws_ifm.Cells(trow, 2)
debug.print host
If the string and the substring are exactly the same, the function will return 1, not 0. If you're getting 0 then they are not the same. You can prove this in the immediate window: ?instr("Dog","Dog") and hit enter at the end of that line.
 
Upvote 0
Solution
That should return 1 if both values are exactly the same, which suggests that they aren't.
 
Upvote 0
what if you write the instr like this?

VBA Code:
InStr(ws_ifm.Cells(trow, 2), host)
 
Upvote 0
Thank you everyone for the diagnostic advice. It turns out that the values were not equal. The value in the cell had a leading space. I used a string length to compare the two and fopund one was one character longe than the other.
JEC, that worked now that I found the mismatch, not sure how it would have with the error. Probably resuult in zero as well.
 
Last edited:
Upvote 0
Glad we could help & thanks for the recognition.
 
Upvote 0
Without specifying the final argument it will use the default which is case sensitive
 
Upvote 0
Unless you have
VBA Code:
Option Compare Text

at the top of the module of course. ;)
 
Upvote 0

Forum statistics

Threads
1,226,112
Messages
6,189,036
Members
453,520
Latest member
packrat68

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