VBA Macro for adding a blank row after comparing Data

tlaltmey

New Member
Joined
Nov 10, 2021
Messages
20
Office Version
  1. 365
  2. 2019
Hi,

I'm looking at creating a basic macro that compares data from Column D. The goal is to add a blank row when the data changes, but only for the first 7 digits.

For example:

1.) 0044982-001
2.) 0044982-002
3.) 0044982-003
4.) 0045000-001
5.) 0045000-002
6.) 0012345-001

Then becomes
1.) 0044982-001
2.) 0044982-002
3.) 0044982-003
4.) 0045000-001
5.) 0045000-002
6.) 0012345-001

Currently I have the following:

VBA Code:
Dim lRow As Long
For lRow = Cells(Cells.Rows.count, "D").End(xlUp).Row To 2 Step -1
    If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then
        Rows(lRow).EntireRow.Insert
        
        ' Adds a gray filled bar in empty rows
        Rows(lRow).Interior.ColorIndex = 48
    End If
Next lRow

This code adds a space between all data, as the last digit continues to change, which is not what I want.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the Board!

If you only want to look at the first 7 characters, try changing this line:
VBA Code:
    If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then
to
VBA Code:
    If Left(Cells(lRow, "D"),7) <> Left(Cells(lRow - 1, "D"),7) Then
 
Upvote 0
Solution
Hi & welcome to MrExcel.
How about
VBA Code:
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row To 2 Step -1
    If Left(Cells(lRow, "D"), 7) <> Left(Cells(lRow - 1, "D"), 7) Then
        Rows(lRow).EntireRow.Insert
        
        ' Adds a gray filled bar in empty rows
        Rows(lRow).Interior.ColorIndex = 48
    End If
Next lRow
 
Upvote 0
Welcome to the Board!

If you only want to look at the first 7 characters, try changing this line:
VBA Code:
    If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then
to
VBA Code:
    If Left(Cells(lRow, "D"),7) <> Left(Cells(lRow - 1, "D"),7) Then
Thanks Joe,

This is exactly what I was looking for.
 
Upvote 0
You are welcome.
Glad we were able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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