See if this does what you want.Thank you for the code. I greatly appreciate it. Would it be possible for the user to select rows 5-16 and then have all other rows deleted or select rows 21-35 and have all other rows deleted?
Only in limited circumstances... my code works also with non contiguous selection,
Excel Workbook | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | 1 | data | data | data | data | ||
2 | 2 | data | data | data | data | ||
3 | 3 | data | data | data | data | ||
4 | 4 | data | data | data | data | ||
5 | 5 | data | data | data | data | ||
6 | 6 | data | data | data | data | ||
7 | 7 | data | data | data | data | ||
8 | 8 | data | data | data | data | ||
9 | 9 | data | data | data | data | ||
10 | 10 | data | data | data | data | ||
Sheet1 |
Yes, I think this does disjoint selections and without a helper sheet... it's possible to avoid the use of a support sheet ?
Sub RemoveUnselectedRows()
Dim lr As Long, fr As Long, i As Long
Dim addr As String
Dim Bits
Bits = Split(Replace(Replace(Selection.Address, ",", ""), ":", ""), "$")
fr = Rows.Count
Application.ScreenUpdating = False
For i = 0 To UBound(Bits)
If IsNumeric(Bits(i)) Then
If Bits(i) > lr Then lr = Bits(i)
If Bits(i) < fr Then fr = Bits(i)
End If
Next i
On Error Resume Next
Rows(lr + 1 & ":" & Rows.Count).Delete
On Error GoTo 0
For i = lr - 1 To fr + 1 Step -1
If Intersect(Rows(i), Selection) Is Nothing Then
Rows(i).Delete
End If
Next i
On Error Resume Next
Rows("1:" & fr - 1).Delete
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
Excel Workbook | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | 1 | data | data | data | data | ||
2 | 2 | data | data | data | data | ||
3 | 3 | data | data | data | data | ||
4 | 4 | data | data | data | data | ||
5 | 5 | data | data | data | data | ||
6 | 6 | data | data | data | data | ||
7 | 7 | data | data | data | data | ||
8 | 8 | data | data | data | data | ||
9 | 9 | data | data | data | data | ||
10 | 10 | data | data | data | data | ||
Sheet3 |