+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    01-22-2010
    Location
    Newfoundland, Canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    VBA-Insert rows and fill number series

    I've struggled with online examples for several days now. The closest example by its description is the "Insert and Fill series" available here, but it does not activate in Excel 07 (written originally for 97)

    Task at hand. Any advice?
    I need to insert rows where a series of numbers, as part of a row of data, are not consecutive. I prefer to run the macro on activecells, since I have some fractional numbers in the overall series (such as 368.1, 368.2, 369).

    Currently: (X=other associated data)

    1 X 45 X...X
    2 X 48 X...X
    3 X 49 X...X
    4 X 51 X...X

    Desired:
    1 X 45 X...X
    2 46
    3 47
    4 X 48 X...X
    5 X 49 X...X
    6 50
    7 X 51 X...X

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7
    Posts
    1,958

    Re: VBA-Insert rows and fill number series

    Try this maco:
    Code:
    Sub test()
        Dim WorkRows        As Long, _
            Ndx             As Long, _
            Diff            As Long, _
            InsertCounter   As Integer, _
            WorkColumn      As String
            
        WorkColumn = "G"    '  <<<<<<<  CHANGE TO YOUR COLUMN
        
        WorkRows = Cells(Rows.Count, WorkColumn).End(xlUp).Row
        
        'Start at the bottom of the list and work up to the top
        'that way ndx will always poin to the row just above the ones
        'that were inserted
        
        For Ndx = WorkRows To 2 Step -1
            Cells(Ndx, WorkColumn).Activate
            Diff = Cells(Ndx, WorkColumn).Value - Cells(Ndx - 1, WorkColumn).Value
            If Diff > 1 Then
                For InsertCounter = 1 To Diff - 1
                    Range(WorkColumn & Ndx).EntireRow.Insert
                    ActiveCell.Value = ActiveCell.Offset(1, 0).Value - 1
                Next InsertCounter
            End If
        Next Ndx
    End Sub
    
    ---
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    01-22-2010
    Location
    Newfoundland, Canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: VBA-Insert rows and fill number series

    It took a few tests and verifying my series, but it did work, thanks!

    I had to take large chunks of the data into a new worksheet to run the macro, since I found several areas where the number series contained a non-sequential number (either a needed repeat or a decimal). Cut'n'paste put everything back to rights.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0