Finding the last one

  • Thread starter Thread starter carlos_ray86
  • Start date Start date
C

carlos_ray86

I have colums and rows of data. The data is grouped in four rows down
two columns across. I sometime have repeating data. I am trying ot
figure out a way so that it looks through the colum find the first and
the last then deletes all but the last.

Ex.

Layer1
Layer2
Layer3
Layer4
Layer5 <----delete this one
Layer5 <----delete this one
Layer5 <----delete this one
Layer5 <--- keep this one

It's always the last part of the data. Layer five is the highest it
will go if that help. Thanks.
 
This is very easy. Too easy

RowCount = 1
do while cells(RowCount,"A").value <> ""
if cells(RowCount,"A").value = _
cells(RowCount + 1,"A").value then

Rows(RowCount).delete
else
RowCount = RowCount + 1
end if
loop
 
I have colums and rows of data. The data is grouped in four rows down
two columns across. I sometime have repeating data. I am trying ot
figure out a way so that it looks through the colum find the first and
the last then deletes all but the last.

Ex.

Layer1
Layer2
Layer3
Layer4
Layer5 <----delete this one
Layer5 <----delete this one
Layer5 <----delete this one
Layer5 <--- keep this one

It's always the last part of the data. Layer five is the highest it
will go if that help. Thanks.

Something like this?
---
Dim Rng As Range, n As Long, i As Long

Set Rng = ActiveSheet.Range("B3:B100")
n = Rng.Cells(Rng.Rows.Count, 1).End(xlUp).Row - Rng.Cells(1, 1).Row + 1
For i = n - 1 To 2 Step -1
If Rng.Cells(n, 1) <> Rng.Cells(i - 1, 1) Then Exit For
Next
If i < n Then
Rng.Range(Rng.Cells(i - 1, 0), Rng.Cells(n - 1, 0)).Delete xlShiftUp
End If
 
How would I do it for the case that I have to look at every 4th row,
not every row.
 
Instead of my original code change the count increment from 1 to 4
from
RowCount = 1
do while cells(RowCount,"A").value <> ""
if cells(RowCount,"A").value = _
cells(RowCount + 1,"A").value then

Rows(RowCount).delete
else
RowCount = RowCount + 1
end if
loop

to:
RowCount = 1
match = False
do while cells(RowCount,"A").value <> ""
if cells(RowCount,"A").value = _
cells(RowCount + 1,"A").value then

Rows(RowCount).delete
MAtch = true
else
if Match = true then
RowCount = RowCount + 1
Mtch = False
else
RowCount = RowCount + 4
end if
loop
 
How would I do it for the case that I have to look at every 4th row,
not every row.

Like Joel already said, you have to move by 4, not 1:
ie.
For i = n - 1 To 2 Step -1
should be
For i = n - 1 To 2 Step -4

Regards,

B.
 

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