Insert blank row if cells match

ckdragon

New Member
Joined
Apr 3, 2022
Messages
37
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys,

Just wondering if there is a simple If statement I can add to some exisiting VBA code I have that states,

If cell = the cell above, then insert blank row.

I already have code they adds rows based on other conditions, but there seems to be the odd error where that insert row fails, so this is a back up.

please and thank you!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Is this what you mean?

VBA Code:
Sub Insert_Rows()
  Dim r As Long
  
  Application.ScreenUpdating = False
  For r = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
    If Range("A" & r).Value = Range("A" & r - 1).Value Then Rows(r).Insert
  Next r
  Application.ScreenUpdating = True
End Sub

Before:

ckdragon_1.xlsm
A
1Data
2a
3b
4b
5b
6c
7c
8d
9
Sheet1


After:

ckdragon_1.xlsm
A
1Data
2a
3b
4
5b
6
7b
8c
9
10c
11d
12
Sheet1
 
Upvote 0
Solution
Ho
Is this what you mean?

VBA Code:
Sub Insert_Rows()
  Dim r As Long
 
  Application.ScreenUpdating = False
  For r = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
    If Range("A" & r).Value = Range("A" & r - 1).Value Then Rows(r).Insert
  Next r
  Application.ScreenUpdating = True
End Sub

Before:

ckdragon_1.xlsm
A
1Data
2a
3b
4b
5b
6c
7c
8d
9
Sheet1


After:

ckdragon_1.xlsm
A
1Data
2a
3b
4
5b
6
7b
8c
9
10c
11d
12
Sheet1
100% perfect again.

Thank you so much!

And thank you for coming back so quickly... Honestly means the world!
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

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