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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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
Hi All,
I'm still learning to make macros, I want to change the macro that @James006 made, the data above becomes horizontal from the range A2:J2 and the results to the range M2:V2 starting from numbers 0 to 9. Please help.
1719219845795.png
 
Upvote 0
T202406a.xlsm
ABCD
1NumberMissing
28
391313
4101515
5111616
6121717
7141919
818
920
4a
Cell Formulas
RangeFormula
C3:C7C3=LET(a,SEQUENCE(13,,8,1),FILTER(a,NOT(COUNTIF(A2:A9,a))))
D3:D7D3=LET(L,A2:A9,a,SEQUENCE(MAX(L)-MIN(L)+1,,MIN(L),1),FILTER(a,NOT(COUNTIF(L,a))))
Dynamic array formulas.



T202406a.xlsm
ABCDEFGHIJKL
1
201346789
325
4
4aa
Cell Formulas
RangeFormula
K3:L3K3=TRANSPOSE(LET(x,A2:J2,a,SEQUENCE(MAX(x)-MIN(x)+1,,MIN(x),1),FILTER(a,NOT(COUNTIF(x,a)))))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,221,590
Messages
6,160,657
Members
451,662
Latest member
reelspike

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