Consulting

Results 1 to 9 of 9

Thread: Identify missing numbers in sequence

  1. #1

    Identify missing numbers in sequence

    Good afternoon,
    I need help, I have a column with about 1000 rows that contain values ​​in sequence (10,11,12, ...),
    I needed a macro that checked in this column which numbers may be missing and which
    these numbers were shown in a MsgBox.

    Thanks

    RomuloRDM

  2. #2
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    This code will check the A column, adjust to suit:
    Sub missingno()
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    inarr = Range(Cells(1, 1), Cells(lastrow, 1))
    msg = "Numbers missing are: "
    For i = 1 To lastrow
       If inarr(i, 1) <> i Then
        msg = msg & i & " , "
       End If
    Next i
    MsgBox (msg)
    End Sub

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Option Explicit
    
    
    Sub MissingNumbers()
        Dim A As Variant
        Dim i As Long, o As Long
        Dim s As String
        Dim r As Range
        
        Set r = ActiveSheet.Range("C3")     '   <<<<<<<<<<<<<<<<<<<<<<<<< change
        Set r = Range(r, r.End(xlDown))
        
        A = Application.WorksheetFunction.Transpose(r.Value)
    
    
        i = A(LBound(A))
        o = 1
    
    
        Do While i < A(UBound(A))
            If A(o) <> i Then
                s = s & i & ","
            Else
                o = o + 1
            End If
            
            i = i + 1
        
        Loop
        
        s = Left(s, Len(s) - 1)
        
        MsgBox s
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by offthelip View Post
    This code will check the A column, adjust to suit:
    Sub missingno()
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    inarr = Range(Cells(1, 1), Cells(lastrow, 1))
    msg = "Numbers missing are: "
    For i = 1 To lastrow
       If inarr(i, 1) <> i Then
        msg = msg & i & " , "
       End If
    Next i
    MsgBox (msg)
    End Sub
    That assumes that the numbers start at 1, and that are are no multiple skips (10,11,12,15,16, ...)


    Capture2.JPG Capture.JPG
    Last edited by Paul_Hossler; 10-16-2019 at 06:36 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    This code suffices:

    Sub M_snb()
        sn = Columns(3).SpecialCells(2, 1)
        sp = Evaluate("transpose(row(" & sn(1, 1) & ":" & sn(UBound(sn), 1) & "))")
        
        For j = 1 To UBound(sn)
           sp(sn(j, 1) - sn(1, 1) + 1) = "_"
        Next
        
        MsgBox Join(Filter(sp, "_", 0), vbLf)
    End Sub

  6. #6
    Good afternoon,
    There was a situation I didn't expect, having two lines with the same value and when this happens all lines down are considered out of sequence, would there be any way around?
    Thanks

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    The give solutions are providing that .
    Why don't you use any one of them ?

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by Romulo Avila View Post
    Good afternoon,
    There was a situation I didn't expect, having two lines with the same value and when this happens all lines down are considered out of sequence, would there be any way around?
    Thanks
    Try this

    The '18' is in twice and 19,20, and 21 are missing

    Capture.JPG



    Option Explicit
    
    
    Sub MissingNumbers()
        Dim A As Variant
        Dim i As Long, o As Long
        Dim s As String
        Dim r As Range
        
        Set r = ActiveSheet.Range("C3")     '   <<<<<<<<<<<<<<<<<<<<<<<<< change
        Set r = Range(r, r.End(xlDown))
        
        A = Application.WorksheetFunction.Transpose(r.Value)
    
    
        i = A(LBound(A))
        o = 1
    
    
        Do While i < A(UBound(A))
            
            If A(o) <> i Then
                s = s & i & ","
            Else
                Do While A(o) = A(o + 1) And i < A(UBound(A))
                    o = o + 1
                Loop
                
                o = o + 1
            End If
            
            i = i + 1
        
        Loop
        
        s = Left(s, Len(s) - 1)
        
        MsgBox s
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Good Morning,


    All right, it worked out, thank you so much for your support.


    Hugs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •