After hours of trial and error, I've determined that doing an Application.Vlookup for a text string in a range returns:
- Error 2042 if the text in the search range isn't preceded by an apostrophe
- Returns a value in the search range if the text is preceded by an apostrophe
Both search variables are type 8, string, but only the range value with the apostrophe is considered equal, even though the string being searched for, does not have an apostrophe in it.
Is there a way to have Vlookup consider text without an apostrophe equal? And no, I'd rather not have to add an apostrophe to everything.
Thanks in advance.
PS Made a small program, using a different version of Excel but still getting undesired results...
Excel Program and VLookup results
In the spreadsheet, all the number values were entered by keyboard.
Then used the format cells option to change them to text.
Then added an apostrophe to the second text value '478901' as denoted by the green triangle, top left-hand corner of the cell.
Vlookup in C2 does not recognize A2 as text, even though it has been formatted as such.
Vlookup in C3 does recognize A3 as text and provides the returned value in column 2.
In the macro, the output pretty much says it all.
myData range is confirmed by address call.
VarType calls for values in A2 & A3 confirms excel is treating them as Double-precision floating-point number (5) and String (8), even though both have been formatted to text, although I suspect this is just for display purposes now.
The Application.Vlookup calls for the values in A2 and A3 return same as spreadsheet Vlookup calls.
In my earlier post, I said that both vartypes returned string (8) but that only the one with the apostrophe worked. This was with the work version of excel, which is earlier than this one. So it looks like someone tried to fix this issue or it just ended up different without attention. Either way, something is broken. "444" doesn't equal "'444" except in excel and "444" equals "444" everywhere else but in excel.
Looks like I'll need to test for both.