Delete non-selected rows

  • Thread starter Thread starter SparePersn
  • Start date Start date
S

SparePersn

Maybe I'm just being thick-headed here. Seems this should be so easy. Excel
2002. I want to delete all the rows that are not selected by the user. I
tried this code to no avail:

ShtBtm=cells(65536,"a").end(xlup).row
for a=shtbtm to 2 step-1
if not rows(a).selected then rows(a).delete
next a

That's what I want. Excel doesn't like rows(a).selected. Help!
Thanks!
 
Sub Tester1()
shtbtm = Cells(65536, "a").End(xlUp).Row
For a = shtbtm To 2 Step -1
If Intersect(Rows(a), Selection) Is Nothing Then
Rows(a).Delete
End If
Next a

End Sub
 
I have not got the code- just a suggestion.

1. selection.address returns a comma separated list of all the rows selected.
2. you should know the used range in the worksheet
3. write a loop for eah row of the used range
if there is no INTERSECT of each element of 1 within the loop row,
record the loop row in a variable
4. at the end of the loop, delete the rows recorded.

You can't delete within the loop.
 

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