+ Reply to Thread
Results 1 to 9 of 9

Add numbers missing from 1-30 in a list

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-30-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2013
    Posts
    177

    Add numbers missing from 1-30 in a list

    Hello all,
    I have a list of numbers in range A2:A33 that correlate to a position on a seating plan. For some groups there may only be say 15 students, but I need to make sure that the numbers between 1-30 not used are in the list.

    e.g. if I have 1,2,3,4,5,6,7,8,9,19,20,21,22,23,24 then the code would put 10,11,12,13,14,15,16,17,18,25,26,27,28,29,30 (each on a new row) in the range A2:A33 below the other numbers.

    I have attached a sheet with an example of what I need. Ideally I need it to operate on the current sheet as I'll add in a button on each sheet.

    Thanks in advance

    Luke
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Add numbers missing from 1-30 in a list

    Luke,

    Give this a try:
    Sub tgr()
        
        Const lStartNum As Long = 1
        Const lEndNum As Long = 30
        
        Dim arrNums As Variant
        Dim varNum As Variant
        Dim strNums As String
        Dim lSize As Long
        
        strNums = Join(Application.Transpose(Evaluate("ROW(" & lStartNum & ":" & lEndNum & ")")), " ")
        For Each varNum In Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value)
            strNums = Replace(" " & strNums & " ", " " & varNum & " ", " ")
        Next varNum
        arrNums = Split(WorksheetFunction.Trim(strNums), " ")
        lSize = UBound(arrNums) - LBound(arrNums) + 1
        
        If lSize > 0 Then Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(lSize).Value = Application.Transpose(arrNums)
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Add numbers missing from 1-30 in a list

    Hi,

    Although this doesn't list the missing numbers immediately underneath each other it will identify them. You could then copy and paste as values then sort them.

    In B2 copied to B30

    Formula: copy to clipboard
    =IF(ISERROR(VLOOKUP(ROW(A2),$A$2:$A$31,1,FALSE)),ROW(A2),"")
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    07-30-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2013
    Posts
    177

    Re: Add numbers missing from 1-30 in a list

    Hi both,
    Richard thank you for the formula - will be useful later. In the immediacy I need something for non-technical staff - so the vba hides it.

    tigeravatar - thank you - perfect solution. Merci Bien! Luke

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Add numbers missing from 1-30 in a list

    Hi,

    You could of course encapsulate the method in a procedure. i.e.

    Sub IDMissingNumbers()
        Dim x As Long
        x = Range("A2").CurrentRegion.Rows.Count - 1
        Range("B2") = "=IF(ISERROR(VLOOKUP(ROW(A2),$A$2:$A$30,1,FALSE)),ROW(A2),"""")"
        Range("B2").Copy Destination:=Range("B2:B31")
        Range("B2:B31").Copy
        Range("A100").End(xlUp).Cells(2, 1).PasteSpecial (xlPasteValues)
        Range("A" & x + 2 & ":A60").Sort key1:=Range("A" & x + 2), Header:=xlNo
        Range("B2:B31").Clear
    End Sub

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Add numbers missing from 1-30 in a list

    If you'll never change the parameters away from looking for "1 to 30", this condensed version will also work, though it may be less readable...
    Sub tgr()
        
        Dim arrNums As Variant
        Dim lSize As Long
        
        arrNums = Filter(Application.Transpose([IF(COUNTIF(A:A,ROW(1:30))=0,ROW(1:30))]), False, False)
        lSize = UBound(arrNums) - LBound(arrNums) + 1
        
        If lSize > 0 Then
            With Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(lSize)
                .Value = Application.Transpose(arrNums)
                .Font.Color = vbWhite
            End With
        End If
        
    End Sub

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Add numbers missing from 1-30 in a list

    PM received from lukestkd:

    Quote Originally Posted by lukestkd
    Hi TigerAvatar,
    A quick question. What code would I need to add in to set the font colour of the numbers added to white?

    I have ActiveSheet.Font.Color = vbWhite

    But I'm not sure how to only apply it to the numbers that have been added in?

    If its not possible it's ok - it'd just neaten it up visually

    Many thanks!

    Luke
    First the ugly part From the Forum Rules:
    4. Don't Private Message or email Excel questions to moderators or other members. (or Word, Access, etc.) The point of having a public forum is to share solutions to common (and sometimes uncommon) problems with all members.


    Now that that's out of the way, replace this line of code:
        If lSize > 0 Then Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(lSize).Value = Application.Transpose(arrNums)

    With this instead:
        If lSize > 0 Then
            With Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(lSize)
                .Value = Application.Transpose(arrNums)
                .Font.Color = vbWhite
            End With
        End If

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,681

    Re: Add numbers missing from 1-30 in a list

    Try
    Sub test()
        Dim x, s As String
        With Range("a2", Range("a" & Rows.Count).End(xlUp))
            s = "row(1:30)"
            x = Filter(Evaluate("transpose(if(isnumber(match(" & s & "," & _
                .Address & ",0)),char(2)," & s & "))"), Chr(2), 0)
            If UBound(x) > -1 Then
                .Offset(.Rows.Count).Resize(UBound(x) + 1).Value = _
                    Application.Transpose(x)
            End If
        End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    07-30-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2013
    Posts
    177

    Re: Add numbers missing from 1-30 in a list

    Hi Tiger,
    Thank for the code and the pointer on the PM

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to find missing numbers from a list in excel 2010?
    By prabhakareord in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 03-13-2013, 03:36 PM
  2. macro for extracting missing numbers from a list
    By cubinity in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-15-2011, 07:17 PM
  3. Deriving list of missing serial numbers
    By cimmind in forum Excel General
    Replies: 3
    Last Post: 04-14-2010, 04:22 AM
  4. missing numbers list
    By Cactuskid in forum Excel General
    Replies: 1
    Last Post: 11-19-2008, 06:34 PM
  5. How do i identify missing numbers in a sequential list
    By Chet-a-roo in forum Excel General
    Replies: 4
    Last Post: 08-05-2005, 03:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1