+ Reply to Thread
Results 1 to 18 of 18

Finding Missing Numbers In A Sequence

Hybrid View

Kitty20 Finding Missing Numbers In A... 02-26-2015, 05:22 PM
protonLeah Re: Finding Missing Numbers... 02-26-2015, 05:46 PM
Kitty20 Re: Finding Missing Numbers... 02-26-2015, 05:50 PM
AB33 Re: Finding Missing Numbers... 02-26-2015, 05:49 PM
Kitty20 Re: Finding Missing Numbers... 02-28-2015, 06:37 AM
AB33 Re: Finding Missing Numbers... 02-28-2015, 06:50 AM
Kitty20 Re: Finding Missing Numbers... 02-28-2015, 06:55 AM
AB33 Re: Finding Missing Numbers... 02-28-2015, 06:59 AM
Kitty20 Re: Finding Missing Numbers... 02-28-2015, 07:10 AM
AB33 Re: Finding Missing Numbers... 02-28-2015, 07:26 AM
Kitty20 Re: Finding Missing Numbers... 02-28-2015, 09:55 AM
AB33 Re: Finding Missing Numbers... 02-28-2015, 01:19 PM
Kitty20 Re: Finding Missing Numbers... 02-28-2015, 01:33 PM
Kitty20 Re: Finding Missing Numbers... 02-28-2015, 02:21 PM
AB33 Re: Finding Missing Numbers... 02-28-2015, 02:10 PM
AB33 Re: Finding Missing Numbers... 02-28-2015, 03:17 PM
Kitty20 Re: Finding Missing Numbers... 02-28-2015, 04:20 PM
AB33 Re: Finding Missing Numbers... 02-28-2015, 04:43 PM
  1. #1
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Question Finding Missing Numbers In A Sequence

    Hi,

    I am trying to find the missing numbers in a sequence from a list using a macro, my current code currently does this but only when I type the data directly into excel, when I import the data from a text file and run the Macro I get the "Sub script out of range" error.

    My current code is:

    Dim arrAll As Variant
    Dim arrMiss As Variant
    Dim LR As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim l As Long
     
    LR = Range("A" & Rows.Count).End(xlUp).Row
    ReDim arrMiss(Range("A" & LR) - Range("A1") - LR)
     
        arrAll = Range("A1:A" & LR)
        For i = 1 To LR - 1
        l = 0
            For j = arrAll(i, 1) + 2 To arrAll(i + 1, 1)
            l = l + 1
            arrMiss(k) = arrAll(i, 1) + l
            k = k + 1
            Next j
        Next i
     Columns("B").ClearContents
    Range("B1").Resize(UBound(arrMiss) + 1) = Application.Transpose(arrMiss)

    Does anyone know a way around this? Any help would be appreciated

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,971

    Re: Finding Missing Numbers In A Sequence

    Post a sample with the numbers that are causing the error.

    Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.
    Last edited by protonLeah; 02-26-2015 at 05:49 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    Im justing using a test file at the moment with the numbers:

    101
    102
    103
    105
    106

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    I think this line will give you an error
    ReDim arrMiss(Range("A" & LR) - Range("A1") - LR).
    Try

    Sub tst()
    Dim arrAll As Variant
    Dim arrMiss As Variant
    Dim LR As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim l As Long
     
    LR = Range("A" & Rows.Count).End(xlUp).Row
     arrAll = Range("A1:A" & LR)
    ReDim arrMiss(Range("A" & LR))
    'or this line ReDim arrMiss(1 To UBound(arrAll, 1))
     
        For i = 1 To LR - 1
        l = 0
            For j = arrAll(i, 1) + 2 To arrAll(i + 1, 1)
            l = l + 1
            arrMiss(k) = arrAll(i, 1) + l
            k = k + 1
            Next j
        Next i
     Columns("B").ClearContents
    Range("B1").Resize(UBound(arrMiss) + 1) = Application.Transpose(arrMiss)
    End Sub

  5. #5
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    Hi,

    Thanks for the reply!

    Yes it is that line of code that is causing the error

    I have tried both of the options above and although it doesn't throw an error anymore, now it doesn't tell me which numbers are missing

    This line of code doesn't throw any errors or any missing numbers when run
    ReDim arrMiss(Range("A" & LR))
    This line of code doesn't throw any errors but displays an "#N/A" in the cell B7 when run
    ReDim arrMiss(1 To UBound(arrAll, 1))

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    Because the redim starts at 1, not zero, k requires a value. Change the order of these lines

    arrMiss(k) = arrAll(i, 1) + l
            k = k + 1
    
    INTO
      k = k + 1
     arrMiss(k) = arrAll(i, 1) + l

  7. #7
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    Still get the same "#N/A"

    I ran a step into on the line of code and it says "Run time error 13: Type Mismatch" although I cant see where the data is mismatched

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    You need to remove 1 if you redim it from 1

    Range("B1").Resize(UBound(arrMiss)) = Application.Transpose(arrMiss)

  9. #9
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    Yeah that's what I did before, but I still get the same "#N/A" and "Run time error 13: Type Mismatch" error

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    Please post a sample, but this code works on the sample you posted

    Sub tst()
    Dim arrAll As Variant
    Dim arrMiss As Variant
    Dim LR As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim l As Long
     
    LR = Range("A" & Rows.Count).End(xlUp).Row
     arrAll = Range("A1:A" & LR)
    'ReDim arrMiss(Range("A" & LR))
    ReDim arrMiss(1 To UBound(arrAll, 1))
     
        For i = 1 To LR - 1
        l = 0
            For j = arrAll(i, 1) + 2 To arrAll(i + 1, 1)
            l = l + 1
            k = k + 1
            arrMiss(k) = arrAll(i, 1) + l
            
            Next j
        Next i
     Columns("B").ClearContents
    Range("B1").Resize(UBound(arrMiss)) = Application.Transpose(arrMiss)
    End Sub

  11. #11
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    Hi

    I have attached a copy of the excel file which includes my imported text data, the code works fine if I physically type the data into the spreadsheet, but not when I import it

    Thanks for your help
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    I am sure what do you mean when you import it. I suspect the data which has imported may not be numbers, but this should not matter as long as the data are clean. How and from which system you are importing in to excel? It is most likely that the imported data may have unseen and non-printed characters. You need to use clean and trim functions to clean the data before you apply the code.

  13. #13
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    I am importing it using the Data>Get External Data>From Text File menu on Excel

  14. #14
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    I thought that, that maybe the format of the cell wasn't correct so when I imported it, it gives you the option to import that column as a number (which I used)

    But I have attached a sample of the spreadsheet with the imported data and the text file im getting the data from
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    Could you import them in to excel and attach the sample so we can see what type of data you have? If you are importing text, how can you find missing numbers? Texts are strings not numbers. The data might appear numbers to your eyes, but not to excel. You somehow need to convert them in to numbers or you are not looking for missing number.

  16. #16
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    Kitty,
    Please find attached.
    Sheet1 is your sheet. I have created sheets 2 and 3.
    Sheet 2 is imported with out removing 1004. In sheet-3 I have deleted 104 from the text so as to test for missing numbers. As you can see, the code works and gets 104 in column B.

    This is how I have done it.

    Import-text-delimited- Comma- then "Do not import the first 6 columns by highlighting each column and tick "Do not import box(Skip) then the last column choose "General" format. This should only import a single column which is the last column after the comma.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    02-26-2015
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Finding Missing Numbers In A Sequence

    That works perfectly!

    Thank you very much

    Could you tell me where I was going wrong? I still don't fully understand what the problem was, the only thing different I did was the options when importing it

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Finding Missing Numbers In A Sequence

    My guess is that you imported all the data, but you forgot to change the column you are looking for. For e.g., if you had imported the data as it is, the last column which has the number is the last column. Let's say you have 7 columns, so you change the array
     arrAll = Range("A1:G" & LR)
    
    For j = arrAll(i, 7) + 2 To arrAll(i + 7, 1)
       arrMiss(k) = arrAll(i, 7) + l

+ 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. Macro to add missing numbers in a sequence
    By webfeet2 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-10-2015, 10:34 AM
  2. [SOLVED] Filtering, sorting and finding missing sequence using macro (for big Excel file)
    By TallOne in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 05-31-2014, 11:24 AM
  3. Finding missing dates with time in sequence
    By yuvrajcbe in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-03-2014, 08:29 AM
  4. find missing numbers in a sequence
    By kaytoo in forum Excel General
    Replies: 1
    Last Post: 06-13-2006, 12:15 PM
  5. [SOLVED] Finding numbers missing from a sequence
    By andy in forum Excel General
    Replies: 4
    Last Post: 04-08-2005, 12:06 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