VBAPrincess
Programmer
I was helping someone add the final touches to a long process and ran into a strange problem with InStr. The long process included a macro which is filtering data raw data by client and copying the data to a new worksheet. On the new worksheet, a pivot table of the pasted raw data is created. To the left of the pivot table, there is a column which needs to be populated with a value from another smaller dataset just above the pivot table. The value we are looking for is a short string (maybe 10 char) and it needs to be found in a longer string (maybe 30 char) from the Row Value in the pivot table. We decided to create a function which he could enter and autofill next to the pivot table. (currently he's looking up the item numbers by hand on 15+ plus worksheets)
Example:
Item#A | DescA
1 | XYZ 7 2013
etc...
Item#B | DescB
? | Northwind XYZ 7 2013 Qtr 1 ABC
When I ran the code, the Instr function would return 0 as if it did not find the string so none of the item numbers were captured. However, if I ran in debug mode and tested the Instr function in the immediate window, the function would return a value (like 18 for example). It was very strange! I would check the values to be compared in the immediate window and then use the values returned in a call to InStr.
Any idea what could be wrong? I tested the function in another workbook with other data and it works as expected. I realize the question might be, why didn't you use VLOOKUP but the strings will never be an exact match and I wasn't sure an approximate match would be reliable. If someone has a suggestion for using Excel functions to accomplish the same, let me know. My first reaction when asked, how would I do this was to say use InStr and then Offset. Everyone has their own way to approach a problem. I was frustrated that InStr wasn't working like it was in the immediate window.
Thanks in advance for assistance!
Diana
VBA Princess
-- I'm hoping to grow up to be a Goddess!
Example:
Item#A | DescA
1 | XYZ 7 2013
etc...
Item#B | DescB
? | Northwind XYZ 7 2013 Qtr 1 ABC
Code:
Function GetItemNumber(rLookIn As Range, strCheck As String)
Dim cell As Variant, strFound As String
For Each cell In rLookIn
If InStr(strCheck, cell.Value) > 0 Then
strFound = cell.Offset(0, -1).Value
Exit For
End If
Next
GetItemNumber = strFound
End Function
When I ran the code, the Instr function would return 0 as if it did not find the string so none of the item numbers were captured. However, if I ran in debug mode and tested the Instr function in the immediate window, the function would return a value (like 18 for example). It was very strange! I would check the values to be compared in the immediate window and then use the values returned in a call to InStr.
Any idea what could be wrong? I tested the function in another workbook with other data and it works as expected. I realize the question might be, why didn't you use VLOOKUP but the strings will never be an exact match and I wasn't sure an approximate match would be reliable. If someone has a suggestion for using Excel functions to accomplish the same, let me know. My first reaction when asked, how would I do this was to say use InStr and then Offset. Everyone has their own way to approach a problem. I was frustrated that InStr wasn't working like it was in the immediate window.
Thanks in advance for assistance!
Diana
VBA Princess
-- I'm hoping to grow up to be a Goddess!