+ Reply to Thread
Results 1 to 11 of 11

Insert missing rows into repeating number sequence

Hybrid View

  1. #1
    Registered User
    Join Date
    09-10-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    6

    Insert missing rows into repeating number sequence

    Hi guys,
    I may be asking too much, but I'm hoping someone with far better vBA skills than me can help me out!
    I have a spreadsheet where one column (F) contains a repeating number sequence of 0-23. I need a macro that will insert a row where this sequence is broken and fill in the number in column F. Seems simple so far, however there may be multiple numbers missing in sequence i.e. 1,2,5 where I need 3 and 4 inserted.

    To make things more interesting the pattern always restarts at 0 which could come at any point in the pattern, the only time that I need rows inserted if 0 breaks the pattern is if a 0 follows 21 or 22.
    So for example 1,2,3,4,5,6,7,8,9,10,0,1,2,3,4,5 would be fine but 18,19,20,21,22,0,1,2,3 would need to become
    18,19,20,21,22,23,0,1,2,3.

    I have attached a worksheet with raw data and the expected outcome.


    Cheers guys, any help will be greatly appreciated.
    Budgie300

    Test1.xls
    Last edited by budgie300; 09-10-2012 at 04:30 PM.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Insert missing rows into repeating number sequence

    Set oneCell = Range("F1")
    
    Do Until oneCell.Value = vbNullString
        With oneCell
      
            If (.Offset(1, 0).Value = 0 And (.Value <> 21 And .Value <> 22)) Or (.Offset(1, 0).Value = .Value + 1) Then
                Rem Ok line insert nothing
            Else
                .Offset(1,0).EntireRow.Insert shift:=xlDown
                .Offset(1,0).Value = .Value + 1
            End If
        End With
        Set oneCell = OneCell.Offset(1, 0)
    Loop
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    09-10-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    6

    Talking Re: Insert missing rows into repeating number sequence

    Quote Originally Posted by mikerickson View Post
    Set oneCell = Range("F1")
    
    Do Until oneCell.Value = vbNullString
        With oneCell
      
            If (.Offset(1, 0).Value = 0 And (.Value <> 21 And .Value <> 22)) Or (.Offset(1, 0).Value = .Value + 1) Then
                Rem Ok line insert nothing
            Else
                .Offset(1,0).EntireRow.Insert shift:=xlDown
                .Offset(1,0).Value = .Value + 1
            End If
        End With
        Set oneCell = OneCell.Offset(1, 0)
    Loop
    Thanks for the reply mikerickson, seems to work great on my test data. I'll run it tomorrow on the full data set and let you know how it goes, but its looking perfect at this stage.
    Thanks for your help.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Insert missing rows into repeating number sequence

    Here is an improvement that protects against wrong data and prevents an infinite loop.

    Sub test()
        Dim oneCell As Range
        Set oneCell = Range("F1")
        
        Do Until CStr(oneCell.Offset(1, 0).Value) = vbNullString
            With oneCell
            
                If Not (IsNumeric(.Value)) Or Not (IsNumeric(.Offset(1, 0).Value)) Then
                    MsgBox "fouled data"
                    Exit Sub
                    
                ElseIf (.Offset(1, 0).Value = 0 And (.Value <> 21 And .Value <> 22)) Or (.Offset(1, 0).Value = .Value + 1) Then
                    Rem Ok line insert nothing
                    
                ElseIf Val(.Offset(1, 0).Value) <= Val(.Value) Then
                    MsgBox "fouled data"
                    Exit Sub
                    
                Else
                    .Offset(1, 0).EntireRow.Insert shift:=xlDown
                    .Offset(1, 0).Value = .Value + 1
                End If
                
            End With
            Set oneCell = oneCell.Offset(1, 0)
        Loop
    End Sub

  5. #5
    Registered User
    Join Date
    09-10-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Insert missing rows into repeating number sequence

    Quote Originally Posted by mikerickson View Post
    Here is an improvement that protects against wrong data and prevents an infinite loop.

    Sub test()
        Dim oneCell As Range
        Set oneCell = Range("F1")
        
        Do Until CStr(oneCell.Offset(1, 0).Value) = vbNullString
            With oneCell
            
                If Not (IsNumeric(.Value)) Or Not (IsNumeric(.Offset(1, 0).Value)) Then
                    MsgBox "fouled data"
                    Exit Sub
                    
                ElseIf (.Offset(1, 0).Value = 0 And (.Value <> 21 And .Value <> 22)) Or (.Offset(1, 0).Value = .Value + 1) Then
                    Rem Ok line insert nothing
                    
                ElseIf Val(.Offset(1, 0).Value) <= Val(.Value) Then
                    MsgBox "fouled data"
                    Exit Sub
                    
                Else
                    .Offset(1, 0).EntireRow.Insert shift:=xlDown
                    .Offset(1, 0).Value = .Value + 1
                End If
                
            End With
            Set oneCell = oneCell.Offset(1, 0)
        Loop
    End Sub

    mikerickson I tested both codes you sent and as you suggested the first one did indeed get stuck in an infinite loop, the second did not so thanks very much for your help.

    I noticed that the first code got stuck in infinite loop when encountering the error in column F of a missing 0. So for example 22,23,1 then the code would loop when it tried to insert a 0.
    This is my fault as I originally stated that the pattern always restarts with a 0, I've lots of these data sets and very rarely the 0 is missed after 23. Is there anyway to modify the macro to allow for this and insert a 0 if its missing after a 23.

    I've also noticed in one of my data sets that the very first line of data in F is actually a 1 not a 0. In this case I require a blank row inserted with a 0 before the 1.

    I know I'm asking a lot here as i'm pretty new to code and i'm not great at it but i am attempting to tinker to get it right, however any assistance is greatly appreciated.

    Again thanks for your time, your help has been great. I've again included a worksheet of raw data and expected outcome

    Test2.xls

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Insert missing rows into repeating number sequence

    Is there anyway to modify the macro to allow for this and insert a 0 if its missing after a 23.
    so
    19, 20, 21, 22, 23, 24, 4
    would go to
    19,20,21,22,23,24, 0, 1, 2, 3, 4

    What would
    8, 9, 10, 3 go to

  7. #7
    Registered User
    Join Date
    09-10-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Insert missing rows into repeating number sequence

    Quote Originally Posted by mikerickson View Post
    Is there anyway to modify the macro to allow for this and insert a 0 if its missing after a 23.
    so
    19, 20, 21, 22, 23, 24, 4
    would go to
    19,20,21,22,23,24, 0, 1, 2, 3, 4

    What would
    8, 9, 10, 3 go to

    The pattern is 0-23 so there wont ever be a 24 so the first example should read

    19, 20, 21, 22, 23, 4
    would go to
    19,20,21,22,23, 0, 1, 2, 3, 4

    technically the second example would be:
    8, 9, 10, 3
    would go to
    8.9,10,0,1,2,3

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Insert missing rows into repeating number sequence

    I think this will meet the added requirements.
    Sub test()
        Dim oneCell As Range
        Set oneCell = Range("F1")
        
        If CStr(oneCell.Value) <> "0" Then
            OneCell.Insert Shift:=xlDown
            Range("F1").Value = 0
            Set oneCell = Range("F1")
        End If
    
        Do Until CStr(oneCell.Offset(1, 0).Value) = vbNullString
            With oneCell
            
                If Not (IsNumeric(.Value)) Or Not (IsNumeric(.Offset(1, 0).Value)) Then
                    MsgBox "fouled data"
                    Exit Sub
                    
                ElseIf (.Offset(1, 0).Value = 0 And (.Value <> 21 And .Value <> 22)) Or (.Offset(1, 0).Value = .Value + 1) Then
                    Rem Ok line insert nothing
                    
                ElseIf Val(.Offset(1, 0).Value) <= Val(.Value) Then
                    .Offset(1, 0).EntireRow.Insert shift:=xlDown
                    .Offset(1, 0).Value = 0
                Else
                    .Offset(1, 0).EntireRow.Insert shift:=xlDown
                    .Offset(1, 0).Value = .Value + 1
                End If
                
            End With
            Set oneCell = oneCell.Offset(1, 0)
        Loop
    End Sub

  9. #9
    Registered User
    Join Date
    09-10-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Insert missing rows into repeating number sequence

    mikerickson, that appears to have done the trick! At least with my home based testing anyway. Will thoroughly test tomorrow and let you know how I get on. Thank you so much for all of your help, your time and patience has been much appreciated and I feel I've learned a lot even from just tinkering with your code

  10. #10
    Registered User
    Join Date
    09-10-2012
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Insert missing rows into repeating number sequence

    mikerickson, I fully tested this today and it worked a treat! Thanks for all your help.

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Insert missing rows into repeating number sequence

    I'm glad it worked.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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