I want to change these lines in my excel VBA code to something much faster, instead of looping through all the rows, i did saw examples but could not understand them as i am not a VBA user.
When I used the code in samples (google,this site) I don’t see the proper need I want, I want to search column A and if values found return the values in column B next to the searched values, else return empty.
Most of the code I used returned error when not found and some other mysterious behavior.
My current code to search is:
Dim k As Integer
For k = 2 To sheet2Counter - 1
Dim tmp As String
tmp = ActiveSheet.Range("A" & k).Value
If tmp = tmpstr Then
tmp = ActiveSheet.Range("B" & k).Value
tmp = Replace(tmp, "Q", "A")
mainstringtopaste = mainstringtopaste + tmp + ","
Exit For
End If
Next k
Also let me know if this is a better way or any code that will replace it to be more fast.
Columns in the sheet to be searched are like:
ColumnA ColumnB
trees leaves
oranges fruits
pineapple fruits
leaves trees
So as my above code, trees should be searched and leaves should be returned…
Thank you
Below are two methods that are superior to looping. Both handle a “no-find” case.
VLOOKUPwith error-handling if the variable doesn’t exist (INDEX/MATCHmay be a better route thanVLOOKUP, ie if your two columns A and B were in reverse order, or were far apart)VBAs
FINDmethod (matching a whole string in column A given I use thexlWholeargument)