VLookup

waxb18

Board Regular
Joined
May 31, 2011
Messages
179
Hi having problems with this macro

At the moment it is copying down the formula to cell G5000 but this is a variable sometimes it can be G2000 sometime G10000,

Can someone show me how to edit this so no matter what it pastes down to the last cell of data

Thanks in advance

Sub VlookupRM()
'
' VlookupRM Macro
'
'
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-2],'[All Engineers.xls]Eng List'!C1:C4,3,FALSE)"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G5000")
Range("H2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-3],'[All Engineers.xls]Eng List'!C1:C4,4,FALSE)"
Range("H2").Select
Selection.AutoFill Destination:=Range("H:H")
Range("H:H").Select
ChDir _
"G:\Stats"
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try

Code:
Sub VlookupRM()
'
' VlookupRM Macro
'
'
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

Range("G2:G" & LR).FormulaR1C1 = "=VLOOKUP(RC[-2],'[All Engineers.xls]Eng List'!C1:C4,3,FALSE)"
Range("H2:H" & LR).FormulaR1C1 = "=VLOOKUP(RC[-3],'[All Engineers.xls]Eng List'!C1:C4,4,FALSE)"
ChDir "G:\Stats"
End Sub
 
Upvote 0
Sir VoG

Normally to find last row we can use below code
Code:
LR = Range("A" & Rows.Count).End(xlUp).Row
How below Line works ?
what does * mean ?
this is Last row of which column ?
SearchDirection:=xlPrevious means which column ?
Code:
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
i will appreciate if you explain
 
Upvote 0
I don't know which column has the last value.

* looks for anything.

xlPrevious looks upwards.
 
Upvote 0
Thank u so much ! have check results with below two codes found results matching
Code:
'code 1
Sub test()
Dim LR As Long
LR = ActiveSheet.UsedRange.Rows.Count
MsgBox LR
End Sub
'code 2
Sub test1()
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
MsgBox LR
End Sub
 
Upvote 0
Both of you are geniuses
I thank you both humbly

One issue i am having now though is getting the file to save and close.
What i now want to do is close the Engineer.xls file and Save as the extract to Call Data (Todays Date).xls

Sub apply_vlookups()

Bdir = "G:\Stats\Engineers.xls"
Workbooks.Open (Bdir)

myworkbook = "Call Data Extract TEMPLATE.xls"

Windows(myworkbook).Activate
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("F2:F" & LR).FormulaR1C1 = "=VLOOKUP(RC[-1],'[All Engineers.xls]Eng List'!C1:C4,2,FALSE)"
Range("G2:G" & LR).FormulaR1C1 = "=VLOOKUP(RC[-2],'[All Engineers.xls]Eng List'!C1:C4,3,FALSE)"
Range("H2:H" & LR).FormulaR1C1 = "=VLOOKUP(RC[-3],'[All Engineers.xls]Eng List'!C1:C4,4,FALSE)"

End Sub
 
Upvote 0
Try

Code:
Sub apply_vlookups()

Bdir = "G:\Stats\Engineers.xls"
Workbooks.Open (Bdir)

myworkbook = "Call Data Extract TEMPLATE.xls"

Windows(myworkbook).Activate
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("F2:F" & LR).FormulaR1C1 = "=VLOOKUP(RC[-1],'[All Engineers.xls]Eng List'!C1:C4,2,FALSE)"
Range("G2:G" & LR).FormulaR1C1 = "=VLOOKUP(RC[-2],'[All Engineers.xls]Eng List'!C1:C4,3,FALSE)"
Range("H2:H" & LR).FormulaR1C1 = "=VLOOKUP(RC[-3],'[All Engineers.xls]Eng List'!C1:C4,4,FALSE)"
ActiveWorkbook.SaveAs Filename:="Call Data " & Format(Date, "mm-dd-yyyy") & ".xls"
Workbooks("Engineers.xls").Close
End Sub
 
Upvote 0
It saved just as i want it to

However when closing i get the following error:

Run-Timer error '9':

Subscript out of range
 
Upvote 0
Do you have any other code in the workbook? What happens if you click Debug?
 
Upvote 0
this is auto sub which is included in the workbook

Sub polo()
Open_Extract_and_COPYANDPASTEDATA
VoG
End Sub

When i click Debug i get the same error
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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