running a macro only in first row

Budapest

New Member
Joined
May 22, 2018
Messages
2
Hello all,

I created a simple macro with many columns, and it's used by different people with different number of rows. When there's only one data row filled, the macro cannot run, I get the debug error message. I assume it is because the macro pulls down all functions to every row as long as the data lasts, but when only one row is used, it doesn't work (I mean one header line and a data line).
Please help.

Thanks,
Peter
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Welcome to the Board!

Please post your code so we can see it, and explain what you want to happen in the case where there is only one row of data filled.
 
Upvote 0
Hi, thanks for the guidance.

Here is one example from the file.

Range("U1").Select
ActiveCell.FormulaR1C1 = "BB Price Min/unit"

Range("U2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]=""Before C8"",RC[-9]*0.5,RC[-9]*0.2)"

LR = Range("A666666").End(xlUp).Row
Range("U2").AutoFill Destination:=Range("U2:U" & LR)

Auto fill is not possible in case only one row is used, as it tries to autofill the lines, but I want my macro to be able to handle one line and many lines as well.

Thanks.
 
Upvote 0
Well, you are already calculating the last row number (LR), so just check to see if that is greater than 2, i.e.
Code:
[I]Range("U1").[I]FormulaR1C1 = "BB Price Min/unit"

[I]Range("U2").[I]FormulaR1C1 = _
[I]"=IF(RC[-1]=""Before C8"",RC[-9]*0.5,RC[-9]*0.2)"

[I]LR = Cells(Rows.Count,"A").End(xlUp).Row
IF LR > 2 Then
[I]     Range("U2").AutoFill Destination:=Range("U2:U" & LR)
End If[/I][/I][/I][/I][/I][/I][/I]
Note: I also cleaned up your code a little. You do not need to select the cells to enter formulas into them. You can reference them directly. Reducing Select statements will speed up your code.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,900
Members
449,194
Latest member
JayEggleton

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