Insert Blank Row when Cell Value changes

ultracyclist

Active Member
Joined
Oct 6, 2010
Messages
274
Office Version
  1. 365
Platform
  1. Windows
In Column B, I have repeating cell values. I would l like to insert a blank row whenever the value changes.


I found the following code which targets only a single value. I tried modifying this to meet my needs, but not sure.


Rich (BB code):
Sub InsertRows()
Dim i As Long
i = 2
Do Until Trim(Cells(i, 1)) = ""
If Cells(i, 1) = "991CX" Then
Cells(i, 1).EntireRow.Insert
i = i + 2
Else
i = i + 1
End If
Loop
End Sub<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
<o:p>
</o:p>
<o:p></o:p>

<o:p></o:p>
<o:p>Any thoughts?</o:p>
<o:p></o:p>
<o:p>Thanks,</o:p>
<o:p></o:p>
<o:p>Allen</o:p>
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi Allen.

Try this Macro:

Code:
Sub BlankRow()
Dim change As Range
Set change = ActiveSheet.UsedRange
For i = change.Rows.Count To 2 Step -1
If Cells(i, 2).Value <> Cells(i - 1, 2).Value Then
Rows(i).Insert Shift:=xlDown
End If
Next
End Sub
 
Upvote 0
This macro should do what you want...

Code:
Sub InsertRowsAtValueChangeColumnB()
  Dim X As Long, LastRow As Long
  Const DataCol As String = "B"
  Const StartRow = 2
  LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
  Application.ScreenUpdating = False
  For X = LastRow To StartRow + 1 Step -1
    If Cells(X, DataCol).Value <> Cells(X - 1, DataCol) Then Rows(X).Insert
  Next
  Application.ScreenUpdating = True
End Sub
Note: Make sure the StartRow constant (in the Const statement) is set to the correct starting row number for your data (I assumed 2 in my code above).
 
Upvote 0
Matt/Rick,

Thank you for your examples, both worked perfectly.

Regards,

Allen.
 
Upvote 0

Forum statistics

Threads
1,221,513
Messages
6,160,244
Members
451,632
Latest member
purpleflower26

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