JimmyA

New Member
Joined
Jun 28, 2018
Messages
2
Hi I have a macro which is copying and pasting a vlookup.

How do I get it top running hen the data the spreadsheet runs out? instead of giving it an line number to end on as below:

Sub vlookup()
'
' vlookup Macro
'
'
Range("N2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-13],Lookup!C[-9]:C[-6],4,FALSE)"
Range("P2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-15],Lookup!C[-11]:C[-9],3,FALSE)"
Range("Q2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-16],Lookup!C[-12]:C[-6],7,FALSE)"
Range("R2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-17],Lookup!C[-13]:C[-6],8,FALSE)"
Range("S2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-18],Lookup!C[-14]:C[-6],9,FALSE)"
Range("N2:S2").Select
Selection.Copy
Range("N3:N6000").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("N2").Select
Columns("N:N").EntireColumn.AutoFit
Columns("P:P").EntireColumn.AutoFit
Range("T2").Select
Columns("Q:Q").EntireColumn.AutoFit
Range("U2").Select
Columns("R:R").EntireColumn.AutoFit
Columns("S:S").EntireColumn.AutoFit
Range("N1").Select
End Sub



Thanks Jimmy
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,919
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel
How about
Code:
Sub vlookupmacro()
   Dim Lr As Long
   
   Lr = Range("A" & Rows.Count).End(xlUp).row
   Range("N2:N" & Lr).FormulaR1C1 = "=VLOOKUP(RC[-13],Lookup!C[-9]:C[-6],4,FALSE)"
   Range("P2:P" & Lr).FormulaR1C1 = "=VLOOKUP(RC[-15],Lookup!C[-11]:C[-9],3,FALSE)"
   Range("Q2:Q" & Lr).FormulaR1C1 = "=VLOOKUP(RC[-16],Lookup!C[-12]:C[-6],7,FALSE)"
   Range("R2:R" & Lr).FormulaR1C1 = "=VLOOKUP(RC[-17],Lookup!C[-13]:C[-6],8,FALSE)"
   Range("S2:S&lr").FormulaR1C1 = "=VLOOKUP(RC[-18],Lookup!C[-14]:C[-6],9,FALSE)"
   Columns("N:N").EntireColumn.AutoFit
   Columns("P:P").EntireColumn.AutoFit
   Columns("Q:Q").EntireColumn.AutoFit
   Columns("R:R").EntireColumn.AutoFit
   Columns("S:S").EntireColumn.AutoFit
   Range("N1").Select
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,919
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback
 

Forum statistics

Threads
1,148,108
Messages
5,744,874
Members
423,907
Latest member
zerocool88

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
Top