I need to fill an excel column with a sequential series, in this case from -500 to 1000. I've got a macro to do it, but it takes a lot of lines for something that seems like it should be a single function [something like FillRange(A2:A1502, -500, 1000, 1)]. But if that function exists, I can't find it. Is the following as simple and elegant as it gets?

'Draw X axis scale
Cells(1, 1).Value = "mV"
Cells(2, 1).Value = -500
Cells(3, 1).Value = -499
Cells(4, 1).Value = -498

Dim selection1 As Range, selection2 As Range

Set selection1 = Sheet1.Range("A2:A4")
Set selection2 = Sheet1.Range("A2:A1502")

selection1.AutoFill Destination:=selection2
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
        Step:=1, Stop:=500, Trend:=False
Sub FillASeries()

    With Sheet1.Range("a1")
        .Value = -500
        .AutoFill .Resize(1501, 1), xlFillSeries
    End With

End Sub

I don't think there's a single function, but this is as short as I can make the procedure.


What you're looking for is not a function.

Type -500 in A2 and type "Ctrl+Enter" (That accepts the number and selects the cell)

Then go to the "Edit" Menu --> "Fill" --> "Series"

Set the "Series in" to "Columns"

Set the "Stop Value" to 1000

Hit "OK" and you'll have the result you want.

Sub Test()

    Call NumberSeriesI(Sheet1.Range("A1"), "MySeries", -1000, 500, 1)

End Sub

Sub NumberSeriesI(StartCell As Range, Header As String, FirstN As Integer, LastN As Integer, StepN As Integer)

' Integer version

    Dim i As Integer ' Value
    Dim r As Integer ' row

    StartCell.Cells(1).Value = Header ' Cells(1) makes sure it only uses the first cell of passed-in range,
                                        ' in case you pass in a multi-celled range for StartCell
    i = FirstN
    r = 1

    Application.ScreenUpdating = False ' Much faster, so the screen is not refreshed until all the values in place

    For i = FirstN To LastN Step StepN
        StartCell.Cells(1).Offset(r, 0).Value = i
        i = i + StepN
        r = r + 1
    Next i

    Application.ScreenUpdating = True

End Sub ' NumberSeriesI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.