VBA: Vlookup to replace column of data from table

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,362
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
This code works fine to replace the value in column 2 of Sheet1 with a Vlookup result from Sheet2 column 2.

Code:
Sub DoVlookup()
    Dim ws1     As Worksheet: Set ws1 = Sheets("Sheet1")
    Dim ws2     As Worksheet: Set ws2 = Sheets("Sheet2")
    Dim Lr      As Long: Lr = ws1.Range("B" & Rows.Count).End(xlUp).Row
    Dim rng     As Range: Set rng = ws2.Range("A2:B7")
    Dim i       As Long
    For i = 2 To Lr
        ws1.Range("B" & i).Value = WorksheetFunction.VLookup(ws1.Range("B" & i).Value, rng, 2, 0)
    Next i
End Sub

I ran across some code which uses an Array and Replace to do the same thing, but it was written as a toggle feature which I don't need. Every subsequent time you run the code it replaces the values from Sheet2, either column 1 or column 2 with the value in Sheet1 column 2.

I don't need to swap the values, I would just like to get the value from Sheet2 column 2 to replace the value in Sheet1 column 2.

Could somebody show me how the .Replace line should be modified to work with the array construct?

Code:
Sub test()
    Dim a, i As Long
    Static flg As Boolean
    flg = Not flg
    a = Sheets("Sheets2").Cells(1).CurrentRegion.Resize(, 2).Value
    With Sheets("Sheets1").Columns(2)
        For i = 2 To UBound(a, 1)
            .Replace a(i, IIf(flg, 2, 1)), a(i, IIf(flg, 1, 2)), 1
        Next
        .AutoFit
    End With
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Maybe
Code:
Sub test()
    Dim a, i As Long
    a = Sheets("Sheets2").Cells(1).CurrentRegion.Resize(, 2).Value
    With Sheets("Sheets1").Columns(2)
        For i = 2 To UBound(a, 1)
            .Replace a(i, 1), a(i, 2), xlWhole, , False, , False, False
        Next
        .AutoFit
    End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

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