I'm trying to insert a blank row between 2 rows if the values in A1 <> A2. I was given the code below and it works if the data looks like this a,a,b,b,c,c,d,d,e,e,f,f,g,g,h,h,i,i
. The results are exactly what I'm looking for: a, blank row, a, blank row, b blank row, b, blank row, c, blank row, c, etc.
However, if the data looks like this a,b,c,d,e,f,g,h,i
then I get 8 blank rows between a and b but none anywhere else.
Any ideas why this is happening like this?
Sub Social_Distance()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet2")
Dim lr As Long, MyUnion As Range, xCell As Range
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
For Each xCell In ws.Range("A2:A" & lr)
If xCell.Value <> xCell.Offset(1).Value Then
If Not MyUnion Is Nothing Then
Set MyUnion = Union(MyUnion, xCell.Offset(1))
Else
Set MyUnion = xCell.Offset(1)
End If
End If
Next xCell
If Not MyUnion Is Nothing Then MyUnion.EntireRow.Insert Shift:=xlDown
End Sub