Macro to replace values in a range (similar to vlookup or index/match)

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
Can a macro REPLACE all the numerical values in columns D through L (there will be at least 5,000 rows) with a value from column C?

The value in each cell in columns D through L matches one of the unique values in A2:A155. The value in column C on that same row as the match in column A should replace each original value.

Example 1: The value in E2 below is 3. That matches with the value in cell A4. The value in C4 is "Yellow". The word "Yellow" should replace the "3" in cell E2.

Example 2: The value in G4 below is 14. That matches with the value in cell A15. The value in C15 is "Fred". The word "Fred" should replace the "14" in cell G4.

ABCDEFGHIJKL
1IDItemValueDateProductRegionSalespersonCustomerPriceDiscountWarrantyDistribution
21Product 1Red14037173549606466
32Product 2Green13546153054576266
43Product 3Yellow12336143052576165
54Product 4Blue11547203355576365
65Region 1North8446133053606466
76Region 2South13528223748586165
87Region 3East15126143148586166
98Region 4West12818234044606366
109Person 1Adam12637183550606465
1110Person 2Bill9446153254586465
1211Person 3Carl13137193451576366
1312Person 4Dave7028233947586366
1413Person 5Evan8226163148576465
1514Person 6Fred11316163242606166
1615Person 7Gary7726142948586366
1716Person 8Hank8117183642576365
1817Person 9Ivan15315102544586265
1918Person 10John11136163152576466
2019Person 11Kent10028213847596265
2120Person 12Liam8245112556576265

<colgroup><col><col><col span="11"></colgroup><tbody>
</tbody>

Thanks for any help on this!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
See how next code can help


Code:
Option Explicit


Sub ChangeData()
'
Dim DataDic   As Object
Set DataDic = CreateObject("Scripting.Dictionary")
Dim Rg  As Range
Dim Wkrg As Range


    For Each Rg In Range([A2], Cells(Rows.Count, 1).End(3))
        DataDic.Item(Rg.Value) = Rg(1, 3).Value
    Next Rg
    Set Wkrg = ActiveSheet.UsedRange
    Set Wkrg = Intersect(Wkrg, Wkrg.Offset(1, 3))
    For Each Rg In Wkrg
        With DataDic
            If (.exists(Rg.Value)) Then Rg = .Item(Rg.Value)
        End With
    Next Rg
End Sub
 
Last edited:
Upvote 0
It works wonderfully!

The only minor thing is that the last column I use is L, but it also fills in the word "Value" for all of the rows in column M. I can easily add code to delete column M after your code runs if there not a quick adjustment.

Thanks so much! This is not something I could even come close to doing myself. CJ
 
Upvote 0
Here we are,
Code:
Option Explicit


Sub ChangeData()
'
Dim DataDic   As Object
Set DataDic = CreateObject("Scripting.Dictionary")
Dim Rg  As Range
Dim Wkrg As Range
Const ColLst = "D:L"
    For Each Rg In Range([A2], Cells(Rows.Count, 1).End(3))
        DataDic.Item(Rg.Value) = Rg(1, 3).Value
    Next Rg
    Set Wkrg = ActiveSheet.UsedRange
    Set Wkrg = Intersect(Wkrg, Columns(ColLst))
    Set Wkrg = Intersect(Wkrg, Wkrg.Offset(1, 0))
    Application.ScreenUpdating = False
    For Each Rg In Wkrg
        With DataDic
            If (.exists(Rg.Value)) Then Rg = .Item(Rg.Value)
        End With
    Next Rg
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
If all your columns have about 5000 rows, then this variation should be a bit quicker.
Code:
Sub MakeReplacements()
  Dim d As Object
  Dim a As Variant
  Dim i As Long, j As Long, ub2 As Long
  
  Set d = CreateObject("Scripting.dictionary")
  a = Range("A2:L" & Range("A" & Rows.Count).End(xlUp).Row).Value
  ub2 = UBound(a, 2)
  For i = 1 To UBound(a)
    d(a(i, 1)) = a(i, 3)
  Next i
  For i = 1 To UBound(a)
    For j = 4 To ub2
      If d.exists(a(i, j)) Then a(i, j) = d(a(i, j))
    Next j
  Next i
  Range("A2").Resize(UBound(a), ub2).Value = a
End Sub
 
Upvote 0
Good news, it was a great pleasure!
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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