SimonGeoghegan
Board Regular
- Joined
- Nov 5, 2013
- Messages
- 68
Hi All,
I have the following loop code which filters my data for me. Once filtered, I want to keep the first top 5 rows of information and delete the rest.
The code almost does what I want, in that it filters and deletes but I think there is an issue with the offset because stepping through the code, it seems to change each time (i'm assuming this is because of the previous 5 lines of information that were kept). Can anybody point me in the right direction for amending the code to keep the first 5 rows of filtered data and delete the rest?
Thanks in advance,
Simon
I have the following loop code which filters my data for me. Once filtered, I want to keep the first top 5 rows of information and delete the rest.
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] 'Loop to remove risks except for Top 5
x = 1
Do Until x = 52
Hospital = Workbooks("PERSONAL.XLSB").Worksheets("Sheet1").Range("AG" & x).Value
ActiveSheet.Range("$A$4:$Y$4").AutoFilter Field:=2, Criteria1:=Hospital, Operator:=xlFilterValues
ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select
If Application.WorksheetFunction.Subtotal(3, Range("A4:A10000")) > 6 Then
Set tbl = ActiveCell.CurrentRegion
tbl.Offset(5, 0).Resize(tbl.Rows.Count - 1, _
tbl.Columns.Count).Select
Range(Selection, Selection.End(xlToRight).End(xlDown)).Select
Selection.EntireRow.Delete
End If
Range("A4:Y4").AutoFilter
Range("E5").CurrentRegion.Sort Key1:=Range("E4"), _
DataOption1:=xlSortTextAsNumbers, _
Header:=xlYes
[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] x = x + 1[/FONT]
The code almost does what I want, in that it filters and deletes but I think there is an issue with the offset because stepping through the code, it seems to change each time (i'm assuming this is because of the previous 5 lines of information that were kept). Can anybody point me in the right direction for amending the code to keep the first 5 rows of filtered data and delete the rest?
Thanks in advance,
Simon