Help showing missing numbers in a sequence

Cruiser69

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

Could anyone help with showing missing numbers in a sequence

I have been using the code below which works well.

VBA Code:
Sub Missing_Numbers_in_Sequence()


' This loops through Column A and checks for missing numbers in a sequence.
' Then shows the missing numbers in column C

   Dim iLoop As Long, iLoop2 As Long
Dim Last_Row As Long
Dim Old_Number As Long, New_Number As Long

Last_Row = Range("A5000").End(xlUp).Row
Old_Number = Val(Right(ActiveSheet.Range("A1").Value, 5))
For iLoop = 1 To Last_Row
    New_Number = Val(Right(Worksheets(1).Range("A" & iLoop).Value, 5))
    For iLoop2 = Old_Number To (New_Number - 2)
        ActiveSheet.Range("C" & Range("C5000").End(xlUp).Row + 1).Value = _
            "" & iLoop2 + 1 & ""
    Next iLoop2
    Old_Number = New_Number
Next iLoop


End Sub




The example below shows just 5 & 7 missing, which is correct


A B C
NumberDescriptionMissing Numbers
1Chair5
2Speaker7
3Phone
4TV
6Table
8Soundbar




If the number does not start at 1 as below, it shows all numbers from 1. Then shows the number that is really missing, which is 13

A B C
NumberDescriptionMissing Numbers
8Chair1
9Speaker2
10Phone3
11TV4
12Table5
14Phone6
7
13



Is there a way to just show the missing numbers from the first number entered


Thanks for looking,


Graham
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
See if changing this line:

Code:
ActiveSheet.Range("C" & Range("C5000").End(xlUp).Row + 1).Value = "" & iLoop2 + 1 & ""

to this:

Code:
If iLoop2 + 1 > Application.Min(Range("A1:A" & Last_Row)) Then
    ActiveSheet.Range("C" & Range("C5000").End(xlUp).Row + 1).Value = "" & iLoop2 + 1 & ""
End If
 
Upvote 0
Hello,

Below is a macro for the missing values

VBA Code:
Sub MissingValues()
Dim rng As Range, j As Variant
Dim StartV As Long, EndV As Long, i As Long
Dim k() As Long, Last_Row As Long

Last_Row = Range("A5000").End(xlUp).Row
ReDim k(0)

Set rng = Range("A2:A" & Last_Row)
StartV = Range("A2")
EndV = Range("A" & Last_Row)

    For i = StartV To EndV
        On Error Resume Next
        j = Application.Match(i, rng, 0)
        If IsError(j) Then
            k(UBound(k)) = i
            ReDim Preserve k(UBound(k) + 1)
        End If
    Next i
Range("C1") = "Missing values"
Range("C2:C" & UBound(k) + 1) = Application.Transpose(k)
End Sub

Hope this will help
 
Upvote 0
You could also do it with a worksheet formula. This is an array formula so should be entered without the {} but confirmed with Ctrl+Shift+Enter, not just Enter. If confirmed correctly, Excel will insert the {}. The formula can then be copied down.

Book1
ABC
1NumberMissing
2813
3915
41016
51117
61219
714 
818 
920 
10
Missing Numbers (2)
Cell Formulas
RangeFormula
C2:C9C2{=IFERROR(SMALL(IF(ISNA(MATCH(ROW(INDEX(A:A,A$2):INDEX(A:A,LOOKUP(9.99E+307,A:A))),A$2:A$100,0)),ROW(INDEX(A:A,A$2):INDEX(A:A,LOOKUP(9.99E+307,A:A))),""),ROWS(C$2:C2)),"")}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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