VBA Vlookup Suggestions

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - to accomplish multiple vlookups I have the below code. It doesn't, well to me, seem very efficient in the VBA world. is there a better more precise way i should be accomplishing the below?

VBA Code:
Sheets("MAIN").Range("I8").Formula = "=VLOOKUP(D8,'FT'!B:J,2,false)"
Sheets("MAIN").Range("J8").Formula = "=VLOOKUP(D8,'FT'!B:J,3,false)"
Sheets("MAIN").Range("K8").Formula = "=VLOOKUP(D8,'FT'!B:J,4,false)"
Sheets("MAIN").Range("L8").Formula = "=VLOOKUP(D8,'FT'!B:J,5,false)"
Sheets("MAIN").Range("M8").Formula = "=VLOOKUP(D8,'FT'!B:J,6,false)"
Sheets("MAIN").Range("N8").Formula = "=VLOOKUP(D8,'FT'!B:J,7,false)"
Sheets("MAIN").Range("O8").Formula = "=VLOOKUP(D8,'FT'!B:J,8,false)"
Sheets("MAIN").Range("P8").Formula = "=VLOOKUP(D8,'FT'!B:J,9,false)"
Sheets("MAIN").Range("Q8").Formula = "=G8-N8"

Sheets("MAIN").Range("I8:Q8").Select
Selection.AutoFill Destination:=Range("I8:Q" & Range("A" & rows.count).End(xlUp).row)
Range(Selection, Selection.End(xlDown)).Select
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Untested, but I think you can replace the entire block you posted with this:
Rich (BB code):
With Sheets("MAIN")
    .Range("I8").Formula = "=VLOOKUP(D8,'FT'!B:J,Column(B:B),false)"
    .Range("I8:P8").FillRight
    .Range("Q8").Formula = "=G8-N8"
End With
Note the hard-coded column number 2 in your formula for cell I8 is replaced by COLUMN(B:B).
 
Upvote 0
Try this.
VBA Code:
With Sheets("MAIN")
    LastRow = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("I8:P" & LastRow).Formula = "=VLOOKUP($D8,'FT'!$B:$J,COLUMNS($I:J),0)"
    .Range("Q8:Q" & LastRow).Formula = "=G8-N8"
End With
 
Upvote 0
Try this.
VBA Code:
With Sheets("MAIN")
    LastRow = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("I8:P" & LastRow).Formula = "=VLOOKUP($D8,'FT'!$B:$J,COLUMNS($I:J),0)"
    .Range("Q8:Q" & LastRow).Formula = "=G8-N8"
End With
getting a variable not defined on "last row"
 
Upvote 0
Add this to your other declarations.
VBA Code:
Dim LastRow As Long
 
Upvote 0
Untested, but I think you can replace the entire block you posted with this:
Rich (BB code):
With Sheets("MAIN")
    .Range("I8").Formula = "=VLOOKUP(D8,'FT'!B:J,Column(B:B),false)"
    .Range("I8:P8").FillRight
    .Range("Q8").Formula = "=G8-N8"
End With
Note the hard-coded column number 2 in your formula for cell I8 is replaced by COLUMN(B:B).
and i would still need to include this?
VBA Code:
Sheets("MAIN").Range("I8:Q8").Select
Selection.AutoFill Destination:=Range("I8:Q" & Range("A" & rows.count).End(xlUp).row)
Range(Selection, Selection.End(xlDown)).Select
 
Upvote 0
and i would still need to include this?
VBA Code:
Sheets("MAIN").Range("I8:Q8").Select
Selection.AutoFill Destination:=Range("I8:Q" & Range("A" & rows.count).End(xlUp).row)
Range(Selection, Selection.End(xlDown)).Select
Yes, if you want to fill the formulas down below row 8.
 
Upvote 0
Careful with those references when copying the formula across.:)
 
Upvote 0

Forum statistics

Threads
1,215,474
Messages
6,125,023
Members
449,203
Latest member
tungnmqn90

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