Add a Row dependent on whether two cells have equal values

James__FinR

New Member
Joined
May 13, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Bit new to VBA, only really using it to make a repetitive task of mine at work much easier!

I have a spreadsheet of raw data that I need to separate so that it is easier to read. Essentially looking to input a function where if value A6 does not equal A5, insert a row between A6 and A5.
e.g.

From:

Apple
Apple
Banana
Banana
Lemon
Cranberry

To:

Apple
Apple

Banana
Banana

Lemon

Cranberry

Hope this isn't too vague. Do ask my questions, though please note I'm quite the amateur.

Appreciate the help!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
How about
VBA Code:
Sub James_FinR()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
      If Cells(i, 1) <> Cells(i - 1, 1) Then Rows(i).Insert
   Next i
End Sub
 
Upvote 0
Way id do this:

VBA Code:
With Sheets("Sheet1")
    frow = 2 'first row of data exc. headers
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    If lrow > frow Then
        For i = lrow To frow + 1 Step -1
            If .Cells(i, 1) <> .Cells(i - 1, 1) And .Cells(i, 1) <> "" And .Cells(i - 1, 1) <> "" Then
                .Rows(i).Insert
            End If
        Next
    End If
End With
 
Upvote 0
Solution
Thanks for the help! Much appreciated.
So the data I'm looking to separate is in column D, I've made edits as below. Though it seems to only be separating column A. Apologies for not specifying this earlier!

VBA Code:
  With Sheets("Realtime Positions")
    frow = 6 'first row of data exc. headers
    lrow = .Range("D" & .Rows.Count).End(xlUp).Row
    If lrow > frow Then
        For i = lrow To frow + 1 Step -1
            If .Cells(i, 1) <> .Cells(i - 1, 1) And .Cells(i, 1) <> "" And .Cells(i - 1, 1) <> "" Then
                .Rows(i).Insert
            End If
        Next
    End If
End With
 
Upvote 0
You need to change the column component of Cells from 1 to 4
 
Upvote 0
Thanks Fluff. Made further edits (Very amateur at this, apologies if I've missed something) but I'm now getting lines between all rows, as opposed to grouping those of the same.

Let me know if I've missed something on the below:

VBA Code:
With Sheets("Realtime Positions")
frow = 6 'first row of data exc. headers
lrow = .Range("D" & .Rows.Count).End(xlUp).Row
If lrow > frow Then
For i = lrow To frow + 1 Step -1
If .Cells(i, 4) <> .Cells(i - 1, 1) And .Cells(i, 4) <> "" And .Cells(i - 1, 1) <> "" Then
.Rows(i).Insert
End If
Next
End If
End With
 
Upvote 0
You need to change the column component of all the cells, not just two of them.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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