Find first and last instance of value

  • Thread starter Thread starter J.W. Aldridge
  • Start date Start date
J

J.W. Aldridge

I want to find the instance of a value and insert a row before and one
after the last instance of the same value.

Data is in column A:G, but the value to find is always in column A.

The first value is "GroupA"
The second value is "GroupB"

Before the first instance of "GroupA", insert row and label "GroupA
Start"
After the last instance of "GroupA", insert row and label "GroupA
End".

Then the same for "GroupB".
 
Sb AddRows()

RowCount = 1
Do while Range("A" & (RowCount + 1)) <> ""
if Range("A" & RowCount) <> Range("A" & (RowCount + 1)) then
Rows(RowCount + 1).Insert
RowCount = RowCount + 2
else
RowCount = RowCount + 1
end if
loop



end Sub
 
This will insert rows at the proper place:

Sub jwa()
n = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To n
If Cells(i, 1).Value = "GroupA" Then
Cells(i, 1).EntireRow.Insert
Exit For
End If
Next
''''''''''''''''''''''''''''''''
For i = n + 1 To 1 Step -1
If Cells(i, 1).Value = "GroupA" Then
Cells(i + 1, 1).EntireRow.Insert
Exit For
End If
Next
End Sub
 

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