PDA

View Full Version : Solved: Delete multiple rows & Unhide hidden rows



adamsm
07-02-2010, 05:09 AM
Hi,
I’m trying to delete the selected (multiple) rows simultaneously from the list box and unhide the hidden rows from the worksheet.
Here’s the code I’m having so far.

Private Sub CommandButton_Delete_Click()
Dim i As Long
Dim LastRow As Long
For i = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(i) Then
With Range("Data").SpecialCells(xlCellTypeVisible)
.Rows(i + 1).Delete
End With
End If
Next i
Range("A6:A" & LastRow).EntireRow.Hidden = False
SheetFilter
End Sub I assume that this might be done by assigning a value to LastRow.

I would be happy if you could let me know how to do this.

Thanks in advance.

Bob Phillips
07-02-2010, 05:13 AM
The problem is, that is the listbox selection is say 6,8 and 22, once 6 is deleted then row 8 isn't row 8 any more, so it will delete the long rows.

Why not use a RefEdit control?

adamsm
07-02-2010, 05:19 AM
Thanks for the reply xld.
My data starts from the 6th row where I have column headings at row 5. I have 7 columns of data.
When I run the code I get the debug message highlighting the line
Range("A6:A" & LastRow).EntireRow.Hidden = False
will a RefEdit control help this?

Bob Phillips
07-02-2010, 05:49 AM
That is because you don't calculate LastRow anywhere, so it will be 0, and A6:A0 is invalid.

adamsm
07-02-2010, 09:42 AM
How may I overcome this?

Bob Phillips
07-02-2010, 09:57 AM
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A6:A" & LastRow).EntireRow.Hidden = False

adamsm
07-02-2010, 12:23 PM
Thanks for the help xld. The code now works. And I do really appreciate your help.