Fill series VBA code

mira2020

New Member
Joined
Sep 25, 2020
Messages
28
Office Version
  1. 2016
Hi all,

i want to write VBA code to fill column A with a series number start with 1, it will be fill until the last value in column B.
Here is my code:

ActiveCell.FormulaR1C1 = "1"
Range("A2").AutoFill Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row), xlFillSeries

it works fine except when the column B has only 1 value , that mean column A should have only "1", there is an error "AutoFil method of RAnge class failed "

Any suggestion? thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How about this?

VBA Code:
Sub fillNum()
    Dim lastRow As Long
    
    lastRow = Range("B" & Rows.Count).End(xlUp).Row
   
    Range("A1").Value = 1
    If lastRow > 1 Then
        Range("A1:A" & lastRow).FillDown
    End If
End Sub
 
Upvote 0
Another variant
VBA Code:
    Dim rng As Range
    Set rng = Range("B2", Range("B" & Rows.Count).End(xlUp))
    
    Range("A2") = 1
    If rng.Rows.Count > 1 Then
        Range("A2").AutoFill rng.Offset(0, -1), xlFillSeries
    End If
 
Upvote 0
thank you @rlv01

i have 2 fill series running on 2 sheets as below, in the same sub:

Dim rng As Range
Set rng = Range("B2", Range("B" & Rows.Count).End(xlUp))

Range("A2") = 1
If rng.Rows.Count > 1 Then
Range("A2").AutoFill rng.Offset(0, -1), xlFillSeries
End If


Dim rng1 As Range
Set rng1 = Range("Q2", Range("Q" & Rows.Count).End(xlUp))

Range("V2") = 1
If rng1.Rows.Count > 1 Then
Range("V2").AutoFill rng1.Offset(0, -1), xlFillSeries
End If

the first one work well, the second one has below error .
not sure what is the problem ?

1696990677039.png
 
Upvote 0
Here is an different approach...
VBA Code:
Sub FillColA()
  With Range("B2", Cells(Rows.Count, "B").End(xlUp))
    .Offset(, -1) = Evaluate("Row(1:" & .Rows.Count & ")")
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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