Autofill goes past Last Row, Last row identifies correct Row

duncanjackson

New Member
Joined
Nov 7, 2019
Messages
2
Hi

I have done lots of Google searches and found nothing on this. Normally I can find an answer on Google, or force the code to do what I want by trying a different method.

I have a VBA problem that is driving me nuts.

I'm using End(xlUp) to find my last row.

When I hover over Last row in the code it correctly identifies the last row as 10687

I then autofill formula's from my 2nd row to Last row...

... And for some reason it fills to Row 210678

Here is my bit of code:

Code:
'Copy formulas down        With Sheets("HMRC")
            LastRow = Range("G" & Rows.Count).End(xlUp).Row
        End With
    Range("A2:E2").Select
            Selection.AutoFill Destination:=Range("A2:E2" & LastRow)
    Range("L2:Y2").Select
            Selection.AutoFill Destination:=Range("L2:Y2" & LastRow)
Does anyone know why the Macro correctly identifies the last row I want to fill to, but then merrily careers past it?

I've also tried the following and the same happens with this method too:

Code:
'Declare variable
'
Dim LR As Long
'
'Copy formulas down
    Range("A2:E2").Select
        LR = Sheets("HMRC").Range("G1").CurrentRegion.Rows.Count
            Selection.AutoFill Destination:=Range("A2:E2" & LR)
    Range("L2:U2").Select
            Selection.AutoFill Destination:=Range("L2:U2" & LR)
Many thanks in advance for anyone who can help!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,177
Office Version
365
Platform
Windows
Welcome to the Board!

If you are tacking on the row number manually, you need to get rid of the current row number, i.e.
Code:
:=Range("L2:U" & LR)
instead of:
Code:
:=Range("L2:U[COLOR=#ff0000]2[/COLOR]" & LR)
Otherwise, if last row was 300, you are actually going down to row 2300!
You need to update that in all those autofill places in your code.
 
Last edited:

duncanjackson

New Member
Joined
Nov 7, 2019
Messages
2
Hi Joe4

Thank you so much! you've cured it. I've been going mad trying to solve this for nearly a week and half!

That was such a simple fix. Thank you!

I love this site! I've been coming here for years and always found the answer I needed.

I am so, so grateful
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,177
Office Version
365
Platform
Windows
You are welcome. Glad I was able to help!
:)
 

Forum statistics

Threads
1,078,437
Messages
5,340,278
Members
399,362
Latest member
iayb

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top