gisvpn
asked on
A quicker way to delete rows?
Hello,
I am using the code below to remove all rows that dont fit within a time window - it takes ages to run!
Is there another, quicker way to do this?
I just want to delete all rows which dont fall within the date range defined here :
Start Date: Sheets("Summary").Range("H 7").Value
End Date: Sheets("Summary").Range("J 7").Value
----
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Data" )
ws.Activate
Dim Row As Long
With ThisWorkbook.Sheets("Data" )
For Row = .UsedRange.Row + .UsedRange.Rows.Count - 1 To .UsedRange.Row Step -1
'MsgBox .Cells(Row, "T").Value
'MsgBox .Cells(Row, "C").Value, vbInformation
'MsgBox Sheets("Summary").Range("H 7").Value, vbCritical
If .Cells(Row, "C").Value < Sheets("Summary").Range("H 7").Value Or .Cells(Row, "C").Value > Sheets("Summary").Range("J 7").Value Then
.Rows(Row).Delete
End If
Next Row
End With
Please Help ;)
I am using the code below to remove all rows that dont fit within a time window - it takes ages to run!
Is there another, quicker way to do this?
I just want to delete all rows which dont fall within the date range defined here :
Start Date: Sheets("Summary").Range("H
End Date: Sheets("Summary").Range("J
----
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Data"
ws.Activate
Dim Row As Long
With ThisWorkbook.Sheets("Data"
For Row = .UsedRange.Row + .UsedRange.Rows.Count - 1 To .UsedRange.Row Step -1
'MsgBox .Cells(Row, "T").Value
'MsgBox .Cells(Row, "C").Value, vbInformation
'MsgBox Sheets("Summary").Range("H
If .Cells(Row, "C").Value < Sheets("Summary").Range("H
.Rows(Row).Delete
End If
Next Row
End With
Please Help ;)
ASKER
Unfortunatley not I would like to do this in VBA ;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
also at the start turn of calculation
and turn it back on after
I am guessing you have screenupdating off too...
and turn it back on after
Application.calculation = xlManual
'... code ...
Application.calculation = xlCalculationAutomatic
I am guessing you have screenupdating off too...
Application.screenupdating = false
Hello,
you can do the autofilter approach with VBA!
cheers, teylyn
you can do the autofilter approach with VBA!
cheers, teylyn
Something along these lines:
cheers, teylyn
Sub DeleteRows()
Dim lRow As Long
Dim FromDate As Date
Dim ToDate As Date
Dim lStartDate As Long
Dim lEndDate As Long
FromDate = Sheets("Summary").Range("H7")
ToDate = Sheets("Summary").Range("J7")
FromDate = DateSerial(Year(FromDate), Month(FromDate), Day(FromDate))
ToDate = DateSerial(Year(ToDate), Month(ToDate), Day(ToDate))
lStartDate = FromDate
lEndDate = ToDate
Sheets("Data").UsedRange.AutoFilter
Sheets("Data").UsedRange.AutoFilter Field:=2, Criteria1:= _
">=" & lStartDate, Operator:=xlAnd, Criteria2:="<=" & lEndDate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
ActiveSheet.UsedRange.AutoFilter Field:=2
Range("A1").Select
Selection.AutoFilter
End Sub
cheers, teylyn
I was also thinking... you coulkd always use ADO and do an SQL Select query to get the data you want. Would allow for the use of:
"SELECT * FROM [DATA] WHERE lDate Between lStart And lEnd;"
"SELECT * FROM [DATA] WHERE lDate Between lStart And lEnd;"
ASKER
Hi teylyn,
I cannot seem to get the filter version to work. It completes but the right rows have not been deleted.
I cannot seem to get the filter version to work. It completes but the right rows have not been deleted.
Maybe I have the filter dates backwards. Maybe you need
Sheets("Data").UsedRange.A utoFilter Field:=2, Criteria1:= _
"<=" & lStartDate, Operator:=xlAnd, Criteria2:=">=" & lEndDate
Sheets("Data").UsedRange.A
"<=" & lStartDate, Operator:=xlAnd, Criteria2:=">=" & lEndDate
Add an autofilter
Filter between the two dates to show the rows to delete.
highlight all the rows that are shown
press [ctrl]+[-] to delete all those rows.