Share via

VBA - Excel Coding Auto Adding Rows

Anonymous
2018-10-03T17:06:29+00:00

wondering if there is a VBA I could use to automatically add rows after data based on a cells value. 

based on the value in column F, I want to add that many blank lines after each row... For Row 9, F9 has a value of 6, so I'd like 6 blank rows. Ideally I'd like the VBA to be a button, I can click, I know how to create the button, just need help with the coding. 

Any help would be greatly appreciated. 

Thank you

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2018-10-04T19:32:30+00:00

    Clearly, you are not allowed to include something in your posts ;)   

    If you want to copy specific information from the row into the new rows, you can try code like this, which will copy the values from columns A:C and G:M into the new rows:

    Sub AddRowsBasedOnColumnF()

        Dim lngR As Long

        For lngR = Cells(Rows.Count, "F").End(xlUp).Row To 2 Step -1

            If IsNumeric(Cells(lngR, "F").Value) Then

                Rows(lngR + 1).Resize(Cells(lngR, "F").Value).Insert

                Intersect(Rows(lngR + 1).Resize(Cells(lngR, "F").Value), Range("A:C")).Value = Intersect(Rows(lngR), Range("A:C")).Value

                Intersect(Rows(lngR + 1).Resize(Cells(lngR, "F").Value), Range("G:M")).Value = Intersect(Rows(lngR), Range("G:M")).Value

            End If

        Next lngR

    End Sub

    0 comments No comments
  2. Anonymous
    2018-10-04T15:40:39+00:00

    To start with my sheet looks like such: 

    The code still works, i just get the error message and the results of the code are: 

    I can live with the error message, since code still works. 

    one last help, would it be possible to have it copy the item # into the rows it adds at the same time? 

    So I get the following: 

    Thanks for your help in advance!!!!

    0 comments No comments
  3. Anonymous
    2018-10-04T14:48:42+00:00

    The only possibility for errors that I can think of are:

    1. the sheet is protected
    2. the sheet is completely filled
    3. the numbers are so large that you try to insert over a million rows in total

    Try unprotecting the sheet and making sure that you have small numbers in column F and running the code again.

    0 comments No comments
  4. Anonymous
    2018-10-04T13:36:38+00:00

    I am getting this error:

    0 comments No comments
  5. Anonymous
    2018-10-03T17:44:21+00:00

    Assign this macro to your button

    Sub AddBlankRowsBasedOnColumnF()

        Dim lngR As Long

        For lngR = Cells(Rows.Count, "F").End(xlUp).Row To 2 Step -1

            If IsNumeric(Cells(lngR, "F").Value) Then

                Rows(lngR + 1).Resize(Cells(lngR, "F").Value).Insert

            End If

        Next lngR

    End Sub

    0 comments No comments