Consulting

Results 1 to 6 of 6

Thread: Inserting multiple rows based on criteria and copy-paste new data

  1. #1
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location

    Inserting multiple rows based on criteria and copy-paste new data

    Hi Everyone,

    I have a list in Excel 2013. I need to insert a new row at the end of every 3 rows and copy-paste new data into column D and E in new rows. Columns A B C should be filled with the same information which repeats in previous rows. I attached a sample spreadsheet with before/after scenarios.

    Can anyone help me on this please?

    Cheers.
    B.
    Attached Files Attached Files
    Last edited by Beatrix; 06-10-2015 at 09:37 AM. Reason: attachment
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Try on your sample sheet:
    Sub blah()
    Sheets("before (2)").Delete
    Sheets("before").Copy After:=Sheets(2)
    Range("A1").CurrentRegion.Subtotal GroupBy:=1, Function:=xlMin, TotalList:=Array(1), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
    With Range("A1").CurrentRegion
      .ClearOutline
      .Columns("A:A").Delete
      With .Columns("B:B").SpecialCells(xlCellTypeBlanks)
        .FormulaR1C1 = "=R[-1]C"
        .Offset(, 1).FormulaR1C1 = "=R[-1]C"
        .Offset(, 2).FormulaR1C1 = Range("H2").Value
        .Offset(, 3).FormulaR1C1 = Range("I2").Value
      End With
      .Rows(.Rows.Count).Delete
      .Value = .Value
    End With
    Columns("E:E").EntireColumn.AutoFit
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i would personally use p45cal's code. it not only is neater but also deals with different number of repetitions in column A.

    below very simple code (which i wrote last night but gave up posting it after seeing p45cal's neater code ) works only with static number of repeats in col A.

    Sub vbax_52858_insert_blank_rows_every_nth_row_fill_from_above()
    
        Dim i As Long, HeaderRow As Long, nth As Long
        
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        HeaderRow = 1 'HeaderRow = 0 if there is no header row
        nth = 3
        
        Worksheets("before").Copy After:=Worksheets("before")
        With ActiveSheet
            .Name = "before_new"
            LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
            
            For i = (LastRow + 1) To (HeaderRow + nth) Step -nth
                Rows(i).Insert
            Next i
            
            LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
            
            For i = (HeaderRow + nth) To LastRow
                If .Range("A" & i).Value = "" Then
                    .Range("A" & i).Value = .Range("A" & i - 1).Value
                    .Range("B" & i).Value = .Range("B" & i - 1).Value
                    .Range("C" & i).Value = .Range("C" & i - 1).Value
                    .Range("D" & i).Value = .Range("H2").Value
                    .Range("E" & i).Value = .Range("I2").Value
                End If
            Next i
        End With
            
        With Application
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    Hi p45cal

    Thanks very much for the code. It works perfect. However I didn't understand the first line below. Is it for deleting worksheet "before"? When I run the code it pops a message to delete worksheet "before" when I press cancel it runs; if I press ok it deletes sheet "before" then gives an error.
    Sheets("before (2)").Delete

    Quote Originally Posted by p45cal View Post
    Try on your sample sheet:
    Sub blah()
    Sheets("before (2)").Delete
    Sheets("before").Copy After:=Sheets(2)
    Range("A1").CurrentRegion.Subtotal GroupBy:=1, Function:=xlMin, TotalList:=Array(1), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
    With Range("A1").CurrentRegion
      .ClearOutline
      .Columns("A:A").Delete
      With .Columns("B:B").SpecialCells(xlCellTypeBlanks)
        .FormulaR1C1 = "=R[-1]C"
        .Offset(, 1).FormulaR1C1 = "=R[-1]C"
        .Offset(, 2).FormulaR1C1 = Range("H2").Value
        .Offset(, 3).FormulaR1C1 = Range("I2").Value
      End With
      .Rows(.Rows.Count).Delete
      .Value = .Value
    End With
    Columns("E:E").EntireColumn.AutoFit
    End Sub
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  5. #5
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Quote Originally Posted by Beatrix View Post
    Hi p45cal

    Thanks very much for the code. It works perfect. However I didn't understand the first line below. Is it for deleting worksheet "before"? When I run the code it pops a message to delete worksheet "before" when I press cancel it runs; if I press ok it deletes sheet "before" then gives an error.
    'Sheets("before (2)").Delete
    It looks like he created a dummy test sheet manually by clicking and dragging and then wrote the code and tweaked it after every run. You can comment that line out and get a new sheet each run with a new name "before (2)", (3), etc.


    Perhaps unrelated but worth noting is the names of these two macros.

    Sub blah()'Where am I?
    Sub vbax_52858_insert_blank_rows_every_nth_row_fill_from_above() 'Oh, THAT post!
    "To a man with a hammer everything looks like a nail." - Mark Twain

  6. #6
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    got it. thanks very much David.
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

Posting Permissions

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