Insert rows in Column using VBA

Dear Experts:

this macro inserts 5 rows whenever the macro hits a blank cell in Column A. I am pretty sure that there is a better way to write this macro, i.e. the line 'Cells(Row, 1).EntireRow.Insert' shouldn't be repeated 5 times.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas


Public Sub InsertRowOnBlank()
    Dim Max As Integer
    Dim Row As Integer

    Max = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For Row = Max To 2 Step -1  ' Start at the bottom, go up
    If Cells(Row, 1).Value = "" Then
            Cells(Row, 1).EntireRow.Insert
            Cells(Row, 1).EntireRow.Insert
            Cells(Row, 1).EntireRow.Insert
            Cells(Row, 1).EntireRow.Insert
            Cells(Row, 1).EntireRow.Insert
        End If
    Next Row
End Sub

Open in new window

Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can simply do this...

Public Sub InsertRowOnBlank()
    Dim Max As Integer
    Dim Row As Integer

    Max = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For Row = Max To 2 Step -1  ' Start at the bottom, go up
    If Cells(Row, 1).Value = "" Then
            Rows(Row & ":" & Row + 3).Insert
                 End If
    Next Row
End Sub

Open in new window


Saurabh....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dsackerContract ERP Admin/ConsultantCommented:
Take out your individual Insert lines. Replace it with:

Range(Cells(Row, 1), Cells(Row + 4, 1)).EntireRow.Insert
Public Sub InsertRowOnBlank()
    Dim Max As Integer
    Dim Row As Integer

    Max = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For Row = Max To 2 Step -1  ' Start at the bottom, go up
        If Cells(Row, 1).Value = "" Then
            Range(Cells(Row, 1), Cells(Row + 4, 1)).EntireRow.Insert
        End If
    Next Row
End Sub

Open in new window

0
Andreas HermleTeam leaderAuthor Commented:
Great job from both of you, both codes work just fine. Thank you very much for your professional support.

Regards, Andreas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.