Logic for vba vlookup from one sheet and apply to another sheet based on range

trpltwo

New Member
Joined
Jan 18, 2023
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello, thank you in advance for your help.

Please refer to attached sample spreadsheet. I would like to add a macro button, to lookup sheet1 tab based on column A "EventID" and lookup the same "EventID" in sheet2 tab and apply column B "Factors" into sheet1 of column C.

Also sheet2 will continue to be updated to add more or amend, so the idea is for the macro to be able to also update/amend based on what's in sheet2.

Noting there is a simple formula without using macro however, reason we need this in VBA is because data is overridden in sheet1 daily and where there's empty cells in column C, some users won't be able to know or confuse other users who are not comfortable manually entering formulas.

thank you again in advance.
 

Attachments

  • Factors sample sheet1.JPG
    Factors sample sheet1.JPG
    139.5 KB · Views: 9
  • Factors sample sheet2.JPG
    Factors sample sheet2.JPG
    100.1 KB · Views: 10

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
One option:

VBA Code:
Option Explicit
Sub Update()
    Dim rng As Range
    Set rng = Sheets("Sheet2").Range("A2", Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp))
    
    With Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row)
        .Value = Application.IfError(WorksheetFunction.VLookup(.Offset(, -2), rng, 2, False), "ID not found")
    End With
End Sub
 
Upvote 0
One option:

VBA Code:
Option Explicit
Sub Update()
    Dim rng As Range
    Set rng = Sheets("Sheet2").Range("A2", Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp))
   
    With Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row)
        .Value = Application.IfError(WorksheetFunction.VLookup(.Offset(, -2), rng, 2, False), "ID not found")
    End With
End Sub
Thanks kevin999 this is great but the code seem to be overriding the ones already there that are not in sheet2, so its replacing the existing one with "ID not found" which should still remain there and only look at replacing the ones that are located in sheet2.
 
Upvote 0
Understood. Try the following instead:
VBA Code:
Option Explicit
Sub Update_V2()
Application.ScreenUpdating = False
    Dim c As Range
    Sheets("Sheet2").Range("A2", Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp)).Name = "rng"
    For Each c In Sheets("Sheet1").Range("C2:C" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row)
        If c = "" Then
            c.FormulaR1C1 = "=iferror(vlookup(RC1,rng,2,false),""ID Not Found"")"
            c = c
        End If
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,907
Messages
6,122,181
Members
449,071
Latest member
cdnMech

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