VBA Formula Autofill

zollan23

New Member
Joined
Jun 26, 2009
Messages
3
Hello -

I am currently trying to run a macro and I need a formula autofilled to the end of my data set and the data set will be a different length each time. I'm trying to enter a formula in cell P2 and I want it to copy down all the way until the data ends.

Here's what I've tried:

Code:
Dim LR As Long
LR = Range("P2").End(xlDown).Row
Range("P2:P" & LR).FormulaR1C1 = "=IF(AND(RC[-4]>-1,RC[-4]<0.1),""Lowest CTR"",IF(AND(RC[-4]>0.1,RC[-4]<0.2),""Extremely Low"",IF(AND(RC[-4]>0.2,RC[-4]<0.3),""Very Low"",IF(AND(RC[-4]>0.3,RC[-4]<0.4),""Low"",IF(AND(RC[-4]>0.4,RC[-4]<0.6),""Below Average"",IF(RC[-4]=0.1,""Lowest"",IF(RC[-4]=0.2,""Extremely Low"",IF(RC[-4]=0.3,""Very Low"",IF(RC[-4]=0.4,""Low"",IF(OR(RC[-4]=0.5,RC[-4]=0.6),""Below Average"",""Error""))))))))))"

This works, however for some reason the formula copies all the way down yet it leaves the last cell blank. I can't figure out why it's working, but leaving that last cell empty. Is there something I'm missing to make it fill all the way down?

I'm new to VBA so I appreciate any help.

Thanks,

Jason Z
 

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.
Hello and welcome to MrExcel.

Try

Code:
LR = Range("P" & Rows.Count).End(xlUp).Row
 
Upvote 0
Hi Jason

You should base how far your formula needs to go down based on some other column (eg column L maybe?):

Code:
Dim LR As Long
LR = Cells(Rows.Count,"L").End(xlUp).Row
Range("P2:P" & LR).FormulaR1C1 = "=IF(AND(RC[-4]>-1,RC[-4]<0.1),""Lowest CTR"",IF(AND(RC[-4]>0.1,RC[-4]<0.2),""Extremely Low"",IF(AND(RC[-4]>0.2,RC[-4]<0.3),""Very Low"",IF(AND(RC[-4]>0.3,RC[-4]<0.4),""Low"",IF(AND(RC[-4]>0.4,RC[-4]<0.6),""Below Average"",IF(RC[-4]=0.1,""Lowest"",IF(RC[-4]=0.2,""Extremely Low"",IF(RC[-4]=0.3,""Very Low"",IF(RC[-4]=0.4,""Low"",IF(OR(RC[-4]=0.5,RC[-4]=0.6),""Below Average"",""Error""))))))))))"
 
Upvote 0
Thanks for the replies everyone. I actually tried this and it worked perfectly!

Code:
Range("P2").FormulaR1C1 = "=IF(AND(RC[-4]>-1,RC[-4]<0.1),""Lowest CTR"",IF(AND(RC[-4]>0.1,RC[-4]<0.2),""Extremely Low"",IF(AND(RC[-4]>0.2,RC[-4]<0.3),""Very Low"",IF(AND(RC[-4]>0.3,RC[-4]<0.4),""Low"",IF(AND(RC[-4]>0.4,RC[-4]<0.6),""Below Average"",IF(RC[-4]=0.1,""Lowest"",IF(RC[-4]=0.2,""Extremely Low"",IF(RC[-4]=0.3,""Very Low"",IF(RC[-4]=0.4,""Low"",IF(OR(RC[-4]=0.5,RC[-4]=0.6),""Below Average"",""Error""))))))))))"
    Range("P2").AutoFill Destination:=Range("P2:P" & Range("O" & Rows.Count).End(xlUp).Row)

thanks again
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

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