Auto insert of blank row depending on data

E

elphix

I'm trying to get myself a macro so I can analyse my data but after
running it a couple of times (and changing info) the results don't
come out as planned. Basically what I'm trying to do is get it to
insert two blank rows when a change in data happens. For example:

1
1
1
1
2
2
2

It should insert two blank rows between the 1 and 2. The problem is
that the data is not in sequence and sometimes is not a number. I need
it to recognise a change regardless of what the value is. Any help/
code would be appreciated
 
G

Gary''s Student

Sub push_row()
Dim v1 As Variant, v2 As Variant
For i = 2 To Rows.Count
If IsEmpty(Cells(i, 1)) Then Exit Sub
v1 = Cells(i, 1).Value
v2 = Cells(i - 1, 1).Value
If v1 <> v2 Then
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
i = i + 2
End If
Next
End Sub
 
R

Roger Govier

Hi

try

Sub Insert2Rows()
Dim c As Long, lrow As Long
With ActiveSheet
lrow = .Cells(.Rows.Count, "A").End(xlUp).Row

For c = lrow - 1 To 1 Step -1
If .Cells(c, 1).Value <> "" Then
If .Cells(c, "A").Value <> .Cells(c + 1, "A").Value Then
.Cells(c + 1, "A").Resize(2).EntireRow.Insert
End If
End If

Next
End With

End Sub
 
R

Roger Govier

I should have added, the code assumes the values are in column A.
If not, change all instances of A"A" to whatever column letter is
applicable.
 
E

elphix

I should have added, the code assumes the values are in column A.
If not, change all instances of A"A" to whatever column letter is
applicable.

Thanks all, I got it working!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top