VBA Lookup from Table and Replace

ryancgarrett

Board Regular
Joined
Jun 18, 2011
Messages
122
I'm trying to create a function that will function similar to vlookup, but will replace the value instead of return it. The other caveat is there are multiple strings to be searched and replaced within a single cell:

ABCDEF
1Lookup TableStringResult
2ChevyAChevy,DodgeA,B
3DodgeBChevy,Toyota,DodgeA,R,B
4ToyotaRToyota,DodgeR,B

<tbody>
</tbody>

I'm thinking something along the lines of =LookReplace(StringCell,LookupTable,LookupResultColumn), i.e. in the table above Cell F2 would contain =LookupReplace(D2,A1:B4,2)

I know I need to use the split function to split the strings and work with the individual strings, but I'm lost as to the lookup portion.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Is this what you want

Formula in F2 copied down

=LookUpReplace(D2,A:A)

Code:
Function LookUpReplace(aString As String, aList As Range)
    Dim arr, c As Long, aMatch As Variant, aStr As String, aVal As String
    arr = Split(aString, ",")

    For c = 0 To UBound(arr)
        aVal = Trim(arr(c))
        On Error Resume Next
            aMatch = aList.Find(aVal, lookat:=xlWhole).Offset(, 1).Value
            If Err.Number > 0 Then aMatch = aVal
            If aStr = vbNullString Then aStr = aMatch Else aStr = aStr & "," & aMatch
        On Error GoTo 0
    Next c
    LookUpReplace = aStr
End Function

Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
G
1
Lookup Table
String Result
2
chevy
A​
Chevy,Dodge A ,B
=LookUpReplace(D2,A:A)
3
Dodge
B​
Chevy,Toyota,Dodge A ,R ,B
4
Toyota
R​
Toyota,Dodge R ,B
5
Toyota, DodgeR ,B
6
Toyota, FordR ,Ford
7
Sheet: Sheet1
 
Upvote 0
Hi

Another option.

With Yongle's layout, in F2:

=LookUpReplace(D2,A:B)

Code:
Function LookUpReplace(s As String, rList As Range) As String
Dim v As Variant
With Application
    v = Split(.Trim(s), ",")
    LookUpReplace = Join(.IfError(.VLookup(v, rList, 2, 0), v), ",")
End With
End Function
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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