Sequential Numbering down to last non blank cell

Podder1965

New Member
Joined
Feb 10, 2021
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Hi,
Is it possible to get sequential numbering in column A starting at Cell A2 (6 consecutive numbers 206240) down to the last Non-blank cell in column B, using vba?
So if cell A2 = 206240
Then cell A10 = 20648
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this in a standard module:
VBA Code:
Sub SeqNum()
    Dim i As Integer, lastRow As Long
    lastRow = Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To lastRow
        Range("A" & i).Value = 206240 + i - 2
    Next i
End Sub
 
Upvote 0
Kanadaaa,
This will work, however, the value 206240 will change, is it possible to have any 6 digit number?
 
Upvote 0
Kanadaaa,
When data is generated into the worksheet it comes from another destination, as such the last number in the destination +1 is then put into cell A2, as mentioned.
So I require this number to be worked from each time (Yes, it will always be in cell A2).
 
Upvote 0
Give this macro a try...
VBA Code:
Sub SequenceA1ValueToLastRowInColumnB()
  With Cells(Rows.Count, "B").End(xlUp)
    Range("A2:A" & .Row) = Evaluate("ROW(A" & [A2] & ":A" & .Row + [A2] & ")")
  End With
End Sub
NOTE: This macro is not dependent on the number in cell A2 being 6 digits long, so if it is possible for that number to be entered otherwise, then you might want to add an If..Then statement to insure the number's size.
 
Last edited:
Upvote 0
Solution
Just try the code below and let me know if it doesn't work the way you want.
VBA Code:
Sub SeqNum()
    Dim i As Integer, lastRow As Long, msg As Long
    lastRow = Range("B" & Rows.Count).End(xlUp).Row
    If Len(Range("A2")) = 6 And IsNumeric(Range("A2")) Then
        For i = 3 To lastRow
            Range("A" & i).Value = Val(Range("A2")) + i - 2
        Next i
    Else
        msg = MsgBox("The value in A2 must be a 6-digit number to run this macro.", vbExclamation, "Error")
    End If
End Sub
 
Upvote 0
Kanadaaa, Rick
Thanks, both codes worked perfectly.
Thanks for your time.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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