Autofill a VLookup Formula to the last row of data

Joined
Nov 19, 2008
Messages
29
Hi,

I am having some trouble creating a macro to copy a vlookup formula from the 1st line of data (G2) all the way to the last row. I can easily create the VLOOKUP statement:

=VLOOKUP(A2,HIST!A$2:H$269,8,FALSE)

However this has 2 problems, firstly the range size will change, depending on the number of rows of data i.e. HIST!A$2:H$269 will not work if there are 300 lines of data.

After I have this issue solved, I then need to find a way of copying that formula down (Autofill) to the last row of data in column G on the page that the formula is running (called HISTFCST)

Any help would be much appreciated
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I currently have:

Dim LR As Integer
Range("H2").FormulaR1C1 = "=VLOOKUP(A2,HIST!A$2:H$269,8,FALSE)
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("H2").AutoFill Destination:=Range("H2:H" & LR), Type:=xlFillDefault
With Range("H2:H" & LR)
End With

But this doesn't work!!
 
Upvote 0
Hi,

You can change your code as below and try again.

Dim LR As Integer
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("H2").Formula = "=VLOOKUP(A2,HIST!A$2:H$" & LR & ",8,FALSE)
Range("H2").Copy
Range("H2:H"&LR).Pastespecial xlPasteAll
Application.CutCopyMode = False
 
Upvote 0
LR = Range("B" & Rows.Count).End(xlUp).Row
Rows.count, counts number of rows in column B, though it is fixed for a version, can be used if the code is used against different versions. This would result 65536 for your case (Excel 2000). From B65536 it moves up to check which is the last row having a value and picks the row number and passes to the variable LR.

Range("H2").Formula = "=VLOOKUP(A2,HIST!A$2:H$" & LR & ",8,FALSE)

This line puts the formula in the cell H2. & symbol is for concatenate. LR is replaced with the number dynamically.

Other two lines are for copy from H2 and then to paste to the rest of the rows in the column H.

Hope it is clear.
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

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