VBA for lookup when no. of rows changes?

1021557

Board Regular
Joined
Dec 14, 2007
Messages
54
Hi, I've written this macro to add a lookup formula in column Z if there is a value in column A and it works fine for this month. The only problem is it will only cover as far as Z 681 as that's how many rows are in column A this month but next month it might be 600 or 847, I simply don't know. How can I get the formula to copy down in column Z depending on the number of values in column A each month? Some sort of an If statement maybe?

Here's the code:

Code:
Range("Z2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-25]<>"""",VLOOKUP(RC[-25],'Lookup sheet'!C[-25]:C[-22],4,FALSE))"
    Range("Z2").Select
    Selection.AutoFill Destination:=Range("Z2:Z681")
    Range("Z2:Z681").Select
    Columns("Z:Z").Select
    Selection.NumberFormat = "mmm-yy"

Thanks

Mike
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try
Code:
   With Range("Z2", Range("A" & Rows.Count).End(xlUp).Offset(, 25))
      .FormulaR1C1 = _
         "=IF(RC[-25]<>"""",VLOOKUP(RC[-25],'Lookup sheet'!C[-25]:C[-22],4,FALSE))"
      .NumberFormat = "mmm-yy"
   End With
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,814
Members
448,990
Latest member
rohitsomani

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