add a blank row after certain cells

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need to add a blank row before when value in B changes, in the example file below, a blank row will need to be added after the highlighted cells.
How do I approach this issue with VBA? Note: the value in column B can be any length, sometimes it is just a number, sometimes it is a combination of text + number.

1672891352939.png
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long, r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row with data in column B
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Check to see if value is different from the row above
        If Cells(r, "B").Value <> Cells(r - 1, "B").Value Then
'           Insert row
            Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long, r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row with data in column B
    lr = Cells(Rows.Count, "B").End(xlUp).Row
   
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Check to see if value is different from the row above
        If Cells(r, "B").Value <> Cells(r - 1, "B").Value Then
'           Insert row
            Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        End If
    Next r
   
    Application.ScreenUpdating = True
   
End Sub
thank you
what if I need to make sure all these added rows have the same value in column A as any other row?
in my example, that would be CF26823
 
Upvote 0
thank you
what if I need to make sure all these added rows have the same value in column A as any other row?
in my example, that would be CF26823
Try this version:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long, r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row with data in column B
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Check to see if value is different from the row above
        If Cells(r, "B").Value <> Cells(r - 1, "B").Value Then
'           Insert row
            Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'           Copy value from row A above
            Cells(r, "A").Value = Cells(r - 1, "A").Value
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Try this version:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long, r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row with data in column B
    lr = Cells(Rows.Count, "B").End(xlUp).Row
   
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Check to see if value is different from the row above
        If Cells(r, "B").Value <> Cells(r - 1, "B").Value Then
'           Insert row
            Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'           Copy value from row A above
            Cells(r, "A").Value = Cells(r - 1, "A").Value
        End If
    Next r
   
    Application.ScreenUpdating = True
   
End Sub
thank you, one last question. The code adds a row after row 1.
What if I want this code to execute from row 2 to last row, would you modify the code like so ?
VBA Code:
    lr = Cells(Rows.Count, "B2:B").End(xlUp).Row
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Check to see if value is different from the row above
        If Cells(r, "B2:B").Value <> Cells(r - 1, "B2:B").Value Then
'           Insert row
            Rows(r).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'           Copy value from row A above
            Cells(r, "A").Value = Cells(r - 1, "A").Value
        End If
    Next r
 
Upvote 0
Change this part:
Rich (BB code):
   For r = lr To 2 Step -1
to this:
Rich (BB code):
   For r = lr To 3 Step -1

Then it won't compare row 2 to row 1 and will stop with comparing row 3 to row 2 (remember we are working backwards, from the bottom to the top).
 
Upvote 0
Change this part:
Rich (BB code):
   For r = lr To 2 Step -1
to this:
Rich (BB code):
   For r = lr To 3 Step -1

Then it won't compare row 2 to row 1 and will stop with comparing row 3 to row 2 (remember we are working backwards, from the bottom to the top).
thanks, this works perfectly !
 
Upvote 0
You are welcome!
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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