We are trying to automatically translate certain part of a string in each Excel cell automatically based on an VBA dictionary.
Original String examples:
1.Outer Fabric:2% EA, 44% WO, 54% PES; Lining:4% EA, 96% RY
Outside:2% EA, 98% WO
1.Outer Fabric:27% PA, 73% WV; 2.Lining:100% CO; 2.Outer Fabric:100% AOS
Regular expression is defined as:
Dim strPattern As String: strPattern = "(\d{1,3}\%\s+)(\w+)"
The dictionary is constructed from another excel spreasheet. The example key/value pairs are:
EA: Leather
WO: Cloth
PES: Polyester
RY: Other
...
But I cannot find a way to use those dictionary keys to replace the original string. Line 12 below is what I tested, but it cannot find the dictionary value...
Dim strPattern As String: strPattern = "(\d{1,3}\%\s+)(\w+)"
Dim strInput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("A2:A50")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
Dim strReplace As String: strReplace = "$1" & IIf(Dict.Exists("$2"), Dict("$2"), "$2")
For Each cell In Myrange
If strPattern <> "" Then
strInput = cell.Value
cell.Value = regex.replace(strInput, strReplace)
End If
Next
Any guidance to get this problem resolved is greatly appreciated. Thanks!