AutoFill series numbers in a column

fhzhkunming

New Member
Joined
Mar 4, 2021
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I want to autofill series numbers in column F. Basically, F2 = 1, F3=2..., the length of the series depends on row numbers of Column G (Fig).

Here is my code, but I got run-time error 1004': Application-defined or object-defined error, which I can't figure out. Could someone help me out?

VBA Code:
Sub Autofill_seriers()
    Dim Lrow As Long
    Lrow = Range("G2:G" & Rows.Count).End(xlUp).Row
    With Sheets("Lat Longs-2")
        Range("F2").Value = 1
        Range("F3").Value = 2
        Range("F2:F3").AutoFill Destination:=.Range("F2:F" & Lrow)  'run-time error '1004'
    End With
      
End Sub

Thanks
 

Attachments

  • AutoFill.JPG
    AutoFill.JPG
    60.5 KB · Views: 8

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
VBA Code:
Sub Autofill_seriers()
    Dim Lrow As Long
    
    With Sheets("Lat Longs-2")
        Lrow = .Range("G" & Rows.Count).End(xlUp).Row
        .Range("F2").Value = 1
        .Range("F3").Value = 2
        .Range("F2:F3").AutoFill Destination:=.Range("F2:F" & Lrow)  'run-time error '1004'
    End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub Autofill_seriers()
    Dim Lrow As Long
   
    With Sheets("Lat Longs-2")
        Lrow = .Range("G" & Rows.Count).End(xlUp).Row
        .Range("F2").Value = 1
        .Range("F3").Value = 2
        .Range("F2:F3").AutoFill Destination:=.Range("F2:F" & Lrow)  'run-time error '1004'
    End With
End Sub
It's working perfectly. Thank you very much.

Frank
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Just as a point of interest, here is another way to write this macro...
Rich (BB code):
Sub AutoFillSeriers()
  With Sheets("Lat Longs-2")
    .Range("F2:F" & .Cells(Rows.Count, "G").End(xlUp).Row) = [ROW(1:1000)]
  End With
End Sub
Note: The above code assumes you will not have more than 1000 rows of data. If you could have more, change the highlighted 1000 to a number that will always be larger than your maximum number of data rows.
 
Upvote 0
Just as a point of interest, here is another way to write this macro...
Rich (BB code):
Sub AutoFillSeriers()
  With Sheets("Lat Longs-2")
    .Range("F2:F" & .Cells(Rows.Count, "G").End(xlUp).Row) = [ROW(1:1000)]
  End With
End Sub
Note: The above code assumes you will not have more than 1000 rows of data. If you could have more, change the highlighted 1000 to a number that will always be larger than your maximum number of data rows.
Thanks, Rick. Good to know that there is another way to do it. But exactly to the Range("F2:F" & Lrow) is perfect to me.

Frank
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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