View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Mallycat[_3_] Mallycat[_3_] is offline
external usenet poster
 
Posts: 1
Default macro to delete rows?


Rather than solve your problem, I will explain how this macro works, so
you can hopefully change it yourself


Set ws1 = Worksheets("ORIGINAL")
This line says that the macro will work on the spreadsheet titled
"ORIGINAL" If you want to run it on another spreadsheet, you will need
to change this line. I suggest you replace this line with
Set ws1 = ActiveSheet

With ws1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row

The above code finds the last row of your data table and selects it.

For r = lastrow To 2 Step -1

The above starts a process where you start at the last row, and then
move up one row at a time until you get to the second to last row (I
assume the last(top) row contains headings).

If Application.And(.Cells(r, "A") = .Cells(r - 1, "A"), .Cells(r,
"C") = .Cells(r - 1, "C"), .Cells(r, "J") = .Cells(r - 1, "J")) Then
..Rows(r).Delete shift:=xlUp

The above code is where all the action happens. Application.And means
all three of the following tests must be true before a row is deleted.
..Cells(r, "A") = .Cells(r - 1, "A")
..Cells(r,"C") = .Cells(r - 1, "C")
..Cells(r, "J") = .Cells(r - 1, "J")

ie .Cells(r, "A") = .Cells(r - 1, "A") says that the current row (data
in column A) must match the row above (data in column A)
etc
etc

So basically if everying in column A, C and J are the exactly the same,
then the bottom row of the 2 rows will be deleted.

If you want to change this so that it checks for the address in column
E, you could either add a new condtion to the end of the
Application.And string like this

If Application.And(.Cells(r, "A") = .Cells(r - 1, "A"), .Cells(r,
"C") = .Cells(r - 1, "C"), .Cells(r, "J") = .Cells(r - 1, "J"),
Cells(r, "E") = .Cells(r - 1, "E"))


Doing this will mean that everything in column A, C, J and E must match
for the deletion to occur. Alternatively if this is not what you want,
just change the formula to suit.

Hope this helps

Matt


--
Mallycat
------------------------------------------------------------------------
Mallycat's Profile: http://www.excelforum.com/member.php...o&userid=35514
View this thread: http://www.excelforum.com/showthread...hreadid=552985