AutoFill going too Far Down.

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
946
Office Version
  1. 2016
Platform
  1. Windows
Hello All,
I have:
VBA Code:
Sheets("HV-1 PVT").Select
Range("AA4").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=AE4+AG4+AI4"
Selection.AutoFill Destination:=Range("AA4:AA" & Range("A" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "0"

This is working
However, I would like it to AutoFill only to last entry in column AB. When I replace Range("A"& Rows.Count...) above with "AB" it still goes all the way to the bottom.
How do I make the AutoFill Stop with the last entry of AB?

Thanks for the help
 
Try running this short macro and tell us what it returns:
VBA Code:
Sub MyTest()
    Sheets("HV-1 PVT").Activate
    MsgBox Cells(Rows.Count, "AB").End(xlUp).Row)
End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Did you try the code I provided?
If you need it wrapped in a procedure for you (I just did a code block like you had in your original question), it would look like this:
VBA Code:
Sub MyAutoFill()

Dim lr as Long

Sheets("HV-1 PVT").Activate

'Find last row with data in column AB
lr = Cells(Rows.Count, "AB").End(xlUp).Row

'Apply formula to entire range at once
Range("AA4:AA" & lr).FormulaR1C1 = "=RC[4]+RC[6]+RC[8]"

'Apply numer format to range
Range("AA4:AA" & lr).NumberFormat = "0"

End Sub

Also, I am not sure what that IF formula in your last post has to do with anything. It does not appear to be part of your original question.
Where does it come in?
I tried the above, and it would compute correctly, but still takes the formula all the way to the bottom. He If code is normal excel and is in a summary sheet. the code always is there, and not part of the VBA code.
 
Upvote 0
Try running this short macro and tell us what it returns:
VBA Code:
Sub MyTest()
    Sheets("HV-1 PVT").Activate
    MsgBox Cells(Rows.Count, "AB").End(xlUp).Row)
End Sub
I have:
Code:
Sheets("HV-1 PVT").Activate
MsgBox Cells(Rows.Count, "AB").End(xlUp).Row

which is working and gives me a message of 50001, which is correct as I have a stop line there, so it doesn't go all the way to the bottom.
 
Upvote 0
I tried this from Answer Is This, and it drops all the way to row 50001, where my stop row is:

Code:
Application.ScreenUpdating = False
Sheets("HV-1 PVT").Select
Range("AA4").Select
Application.CutCopyMode = False
ActiveCell.Formula = "=AE4+AG4+AI4"
Selection.AutoFill Destination:=Range("AA4:AA" & Range("AB" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "0"
 
Upvote 0
I have:
Code:
Sheets("HV-1 PVT").Activate
MsgBox Cells(Rows.Count, "AB").End(xlUp).Row

which is working and gives me a message of 50001, which is correct as I have a stop line there, so it doesn't go all the way to the bottom.
OK, then the code I posted should put the formula in cells AA4:AA50001.
If it is not doing that, I suspect that maybe you have other codes interfering with it or changing it after the fact.

One other possibility. What is the name of the VBA Module that you are trying to run this code from?
 
Upvote 0
OK, then the code I posted should put the formula in cells AA4:AA50001.
If it is not doing that, I suspect that maybe you have other codes interfering with it or changing it after the fact.

One other possibility. What is the name of the VBA Module that you are trying to run this code from?
the module name is: Run_HV1()
is it possible to make it end at the last entry of AB?
Thanks
 
Upvote 0
Upvote 0
OK, it makes no sense why it wouldn't work.
You already confirmed it is going down to line 50001.

Without actually having access to your file to see what is actually going on, I really don't have any other ideas on why it would not be working.
I have a feeling that there may be some else interfering with it or some important detail that is being left out.
 
Upvote 0
OK, it makes no sense why it wouldn't work.
You already confirmed it is going down to line 50001.

Without actually having access to your file to see what is actually going on, I really don't have any other ideas on why it would not be working.
I have a feeling that there may be some else interfering with it or some important detail that is being left out.
Well, I cheated...
This is working:
VBA Code:
Sheets("HV-1 PVT").Activate
With Range("AA4:AA" & Cells(Rows.Count, "AB").End(xlUp).Row)
    .Formula = "=IF(AB4<>"""",AE4+AG4+AI4,"""")"
    .NumberFormat = "0"
End With

Thanks for all the help
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,656
Messages
6,120,762
Members
448,991
Latest member
Hanakoro

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