VBA to insert an empty row based on Column being not blank

Anfinsen

Board Regular
Joined
Apr 11, 2023
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi all, I have the code below, where i want to insert an empty row, where there is data in column F, starting at F6 (info above that is retained header info). The code below just causes excel to freeze up. I would appreciate any assistance in figuring out what I did wrong here! I am almost certain it's the c.EntireRow.Insert that is causing me an issue, I just don't know what the correct solution is?

VBA Code:
Sub AddRows()
  Dim c As Range, sh1 As Worksheet
  Set sh1 = Sheets("Pick List")
  For Each c In sh1.Range("F6", sh1.Cells(Rows.Count, "F").End(xlUp))
    If c.Value <> "" Then
      c.EntireRow.Insert
      
    End If
  Next
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I don't think your range reference is quite right, but in any event, when inserting rows you need to start from the bottom and work your way to the top. Like this:

VBA Code:
Option Explicit
Sub Anfinsen()
    Dim sh1 As Worksheet, LRow As Long, i As Long
    Set sh1 = Worksheets("Pick List")
    LRow = sh1.Cells(Rows.Count, "F").End(xlUp).Row
    
    For i = LRow To 6 Step -1
        If sh1.Cells(i, 6) <> "" Then sh1.Cells(i, 6).EntireRow.Insert
    Next i
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,226,117
Messages
6,189,062
Members
453,524
Latest member
AshJames

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top