Inserting 2 rows above a certain cell

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a column with numbers that are sorted in ascending order e.g

1
1
1
3
3
3
7
7
7

The highest number possible is 8. I would like to insert a line above the
spot where the number changes. How can I do this?
 
Subject is at odds with the text!

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim iLastRow As Long

With ActiveSheet

iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = iLastRow - 1 To 1 Step -1
If .Cells(i, TEST_COLUMN).Value <> .Cells(i + 1,
TEST_COLUMN).Value Then
.Rows(i + 1).Insert
End If
Next i

End With

End Sub


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
I realised the mistake after I posted. Sorry!! Momentary lapse of reason ...

Yes, I did mean insert 2 rows (lines) above the spot where the number
changes i.e. above the first time 3 appears and above the first time 7
appears.

Thank you!
KDJ
 
Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long
Dim iLastRow As Long

With ActiveSheet

iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = iLastRow - 1 To 1 Step -1
If .Cells(i, TEST_COLUMN).Value <> .Cells(i + 1, _
TEST_COLUMN).Value Then
.Rows(i + 1).Resize(2).Insert
End If
Next i

End With

End Sub

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

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

Back
Top