Adding numbers in sequence using VBA

Cruiser69

Board Regular
Joined
Mar 12, 2018
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hi all.

I use the code below to add missing numbers in a sequence.

It works well, but if there is a letter with a number, it fails

EG:
1
2
2A
3
5

Code:
Sub Add_In_Sequence()

  Dim LastRow As Long
Dim gap As Long
Dim i As Long, ii As Long

    Application.Screenupdating = False
    
    With ActiveSheet
    
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = LastRow To 3 Step -1
        
            gap = .Cells(i, "A").Value - .Cells(i - 1, "A").Value
            If gap > 1 Then
            
                .Rows(i).Resize(gap - 1).Insert
            End If
        Next i
        
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Cells(3, "A").Value = .Cells(2, "A").Value + 1
        .Cells(2, "A").Resize(2).AutoFill .Cells(2, "A").Resize(LastRow - 1)
    End With
 
End Sub

It fails at this line

Code:
 gap = .Cells(i, "A").Value - .Cells(i - 1, "A").Value

Is there a way to modify this code to make it work with numbers and letters

Regards,

Graham
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
2A is not a missing number, but do you want to delete it because you have a 2 and a 3 in your list ?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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