Macro with dynamic range selection works for large range of rows but doesn't when the range is just 1 row

patrasgabriela

New Member
Joined
May 9, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have created a Macro that uses a variable count the rows in order to copy paste a formula from Cell 2 of that column on all rows and it works perfectly when the range is made from multiple rows, but doesn't work if the range is just one row. The macro gets stuck at when reading the range. Is there a way to make this work? Below my macro. Thank you in Advance for your help.

VBA Code:
Sub workPF()
lRow = Range("A" & Rows.Count).End(xlUp).Row
       
    Range("AI2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-10]>0,RC[-8]/(RC[-13]*RC[-9]*RC[-10]),RC[-8]/(RC[-13]*RC[-9]))"
    Range("AI2").AutoFill Destination:=Range("AI2:AI" & lRow), Type:=xlFillDefault

End sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub workPF()
lRow = Range("A" & Rows.Count).End(xlUp).Row
       
    Range("AI2:AI" & lRow).FormulaR1C1 = _
        "=IF(RC[-10]>0,RC[-8]/(RC[-13]*RC[-9]*RC[-10]),RC[-8]/(RC[-13]*RC[-9]))"

End Sub
 
Upvote 0
Solution
Hi and thank you for you very prompt reply.

That works perfectly and easier way to write better code using less lines. Thank you :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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