Hi Peter,I'm not so sure. With that method, if you have "pineapples" in the column A list, you will end up with "pinegrapes" in your array. Is that what you want? You did mention 'strings' in your question but I'm wondering if you really meant 'words'?
It is also unclear whether case-sensitivity might come into play with your real data. For example, if "Apples" was in column A that would not get replaced if the column C value is "apples" with the Application.Substitute suggestion. Can you clarify that issue too please?
One more: Is it true with your real data that the original list (column A) consists of single words only, or might some cells have multiple words? If multiple words are possible, would you want "red apples" to become "red grapes" or would the entire cell need to match a column C value?
Sub ReplaceValuesInArray()
Dim d As Object
Dim vSource As Variant, SearchReplace As Variant
Dim i As Long
Set d = CreateObject("Scripting.Dictionary")
vSource = Application.Transpose(Range("A2:A20").Value)
SearchReplace = Range("C2:D4").Value
For i = 1 To UBound(SearchReplace)
d(SearchReplace(i, 1)) = SearchReplace(i, 2)
Next i
For i = 1 To UBound(vSource)
If d.exists(vSource(i)) Then vSource(i) = d(vSource(i))
Next i
End Sub
Sub How_Does_This_Measure_Up()
Dim arrInp, arrSR, i As Long, j As Long
arrInp = Sheets("Sheet2").Range("A2:A" & Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row)
arrSR = Sheets("Sheet2").Range("C2:D" & Sheets("Sheet2").Cells(Rows.Count, 3).End(xlUp).Row)
For j = LBound(arrSR) To UBound(arrSR)
For i = LBound(arrInp) To UBound(arrInp)
If arrInp(i, 1) = arrSR(j, 1) Then arrInp(i, 1) = arrSR(j, 2)
Next i
Next j
Sheets("Sheet2").Cells(2, 6).Resize(UBound(arrInp)).Value = arrInp
End Sub
For me for 6,000 rows in column A and 2000 rows in C:D as mentioned in post #7Just wondering, for only a few thousand cells, how long does this take?
For j = LBound(arrSR) To UBound(arrSR)
For i = LBound(arrInp) To UBound(arrInp)
If arrInp(i, 1) = arrSR(j, 1) Then
arrInp(i, 1) = arrSR(j, 2)
Exit For
End If
Next i
Next j
Peter, I try your VBA code with data in post nr.7 and nothing happened. What I do wrong?Thanks for the additional information. Given your large actual data, I would use 2 loops but they would be separate loops not outer/inner as suggested earlier.
Give something like this a try. Note that I have changed the variable name 'Source' since that is a word already used in the vba language & so is not a good idea to use as a variable name.
VBA Code:Sub ReplaceValuesInArray() Dim d As Object Dim vSource As Variant, SearchReplace As Variant Dim i As Long Set d = CreateObject("Scripting.Dictionary") vSource = Application.Transpose(Range("A2:A20").Value) SearchReplace = Range("C2:D4").Value For i = 1 To UBound(SearchReplace) d(SearchReplace(i, 1)) = SearchReplace(i, 2) Next i For i = 1 To UBound(vSource) If d.exists(vSource(i)) Then vSource(i) = d(vSource(i)) Next i End Sub
Thanks so much @jolivanes and @Peter_SSs. I've tried both of your codes and works perfectly. I think my current data is not big enough to affect the performance.Just wondering, for only a few thousand cells, how long does this take?
Code:Sub How_Does_This_Measure_Up() Dim arrInp, arrSR, i As Long, j As Long arrInp = Sheets("Sheet2").Range("A2:A" & Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row) arrSR = Sheets("Sheet2").Range("C2:D" & Sheets("Sheet2").Cells(Rows.Count, 3).End(xlUp).Row) For j = LBound(arrSR) To UBound(arrSR) For i = LBound(arrInp) To UBound(arrInp) If arrInp(i, 1) = arrSR(j, 1) Then arrInp(i, 1) = arrSR(j, 2) Next i Next j Sheets("Sheet2").Cells(2, 6).Resize(UBound(arrInp)).Value = arrInp End Sub
True. When I did my test with larger data I didn't have any repeat words and forgot about that possibility.Peter.
If it exits the loop after the first found, it'll miss the 2nd, 3rd etc occurrences.
Yes it did. The question was about replacing values in a vba array, which is what my code does. The question did not ask for for anything specific to be done with that array after the replacements had been made, so I didn't do anything. If you stop the code at the End Sub and view what is now in the array, you will see that the relevant values have been changed.Peter, I try your VBA code with data in post nr.7 and nothing happened.
You're welcome. Glad we could help. Thanks for the follow-up.Thanks so much @jolivanes and @Peter_SSs. I've tried both of your codes and works perfectly. I think my current data is not big enough to affect the performance.
Tom.Jones said:
Peter, I try your VBA code with data in post nr.7 and nothing happened.
Yes it did. The question was about replacing values in a vba array, which is what my code does. The question did not ask for for anything specific to be done with that array after the replacements had been made, so I didn't do anything. If you stop the code at the End Sub and view what is now in the array, you will see that the relevant values have been changed.