PC Review


Reply
Thread Tools Rate Thread

Delete all rows not current date

 
 
SITCFanTN
Guest
Posts: n/a
 
      18th Nov 2009
The date in my table is in mm/dd/yy format and located in column E. I need
to delete all rows not the current date quickly. I want to build a macro in
VB to delete all records not the current date. Any help you have is greatly
appreciated, thank you.
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      18th Nov 2009
Give the following macro a try; change my example setup in the three Const
statements to match your actual layout. Note that I interpreted your
reference to "current date" to be today's date; if that was wrong, then
change the <>Date condition in the first If statement to the date value you
actually want to use.

Sub RemoveNotCurrentRecords()
Dim X As Long
Dim LastRow As Long
Dim OriginalCalculationMode As Long
Dim RowsToDelete As Range

Const DataStartRow As Long = 1
Const UnionColumn As String = "E"
Const SheetName As String = "Sheet7"

On Error GoTo Whoops
OriginalCalculationMode = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

With Worksheets(SheetName)
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For X = LastRow To DataStartRow Step -1
If .Cells(X, UnionColumn).Value <> Date Then
If RowsToDelete Is Nothing Then
Set RowsToDelete = .Cells(X, UnionColumn)
Else
Set RowsToDelete = Union(RowsToDelete, .Cells(X, UnionColumn))
End If
If RowsToDelete.Areas.Count > 100 Then
RowsToDelete.EntireRow.Delete xlShiftUp
Set RowsToDelete = Nothing
End If
End If
Next
End With
If Not RowsToDelete Is Nothing Then
RowsToDelete.EntireRow.Delete xlShiftUp
End If
Whoops:
Application.Calculation = OriginalCalculationMode
Application.ScreenUpdating = True
End Sub

--
Rick (MVP - Excel)


"SITCFanTN" <(E-Mail Removed)> wrote in message
news:8542DAE9-E688-466D-9B8D-(E-Mail Removed)...
> The date in my table is in mm/dd/yy format and located in column E. I
> need
> to delete all rows not the current date quickly. I want to build a macro
> in
> VB to delete all records not the current date. Any help you have is
> greatly
> appreciated, thank you.


 
Reply With Quote
 
Paul C
Guest
Posts: n/a
 
      18th Nov 2009
Sub deletedates()

lastrow = Range("e10000").End(xlUp).Row
For A = 1 To lastrow
If Cells(A, 5) <> Today Then Cells(A, 5).EntireRow.delete
Next A

End Sub

--
If this helps, please remember to click yes.


"SITCFanTN" wrote:

> The date in my table is in mm/dd/yy format and located in column E. I need
> to delete all rows not the current date quickly. I want to build a macro in
> VB to delete all records not the current date. Any help you have is greatly
> appreciated, thank you.

 
Reply With Quote
 
Paul C
Guest
Posts: n/a
 
      18th Nov 2009
Sorry - You should work from the bottom to not get caught in a bad loop

Sub deletedates()

lastrow = Range("e10000").End(xlUp).Row
For A = lastrow To 1 Step -1
If Cells(A, 5) <> Today Then Cells(A, 5).EntireRow.delete
Next A

End Sub
--
If this helps, please remember to click yes.


"SITCFanTN" wrote:

> The date in my table is in mm/dd/yy format and located in column E. I need
> to delete all rows not the current date quickly. I want to build a macro in
> VB to delete all records not the current date. Any help you have is greatly
> appreciated, thank you.

 
Reply With Quote
 
SITCFanTN
Guest
Posts: n/a
 
      18th Nov 2009
HI Rick,

This deleted all my records, it didn't just delete the records that were not
dated for today. I only changed my sheet name...I changed your text of
"sheet 7" to "All Records"

Help, thank you.

"Rick Rothstein" wrote:

> Give the following macro a try; change my example setup in the three Const
> statements to match your actual layout. Note that I interpreted your
> reference to "current date" to be today's date; if that was wrong, then
> change the <>Date condition in the first If statement to the date value you
> actually want to use.
>
> Sub RemoveNotCurrentRecords()
> Dim X As Long
> Dim LastRow As Long
> Dim OriginalCalculationMode As Long
> Dim RowsToDelete As Range
>
> Const DataStartRow As Long = 1
> Const UnionColumn As String = "E"
> Const SheetName As String = "Sheet7"
>
> On Error GoTo Whoops
> OriginalCalculationMode = Application.Calculation
> Application.Calculation = xlCalculationManual
> Application.ScreenUpdating = False
>
> With Worksheets(SheetName)
> LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
> For X = LastRow To DataStartRow Step -1
> If .Cells(X, UnionColumn).Value <> Date Then
> If RowsToDelete Is Nothing Then
> Set RowsToDelete = .Cells(X, UnionColumn)
> Else
> Set RowsToDelete = Union(RowsToDelete, .Cells(X, UnionColumn))
> End If
> If RowsToDelete.Areas.Count > 100 Then
> RowsToDelete.EntireRow.Delete xlShiftUp
> Set RowsToDelete = Nothing
> End If
> End If
> Next
> End With
> If Not RowsToDelete Is Nothing Then
> RowsToDelete.EntireRow.Delete xlShiftUp
> End If
> Whoops:
> Application.Calculation = OriginalCalculationMode
> Application.ScreenUpdating = True
> End Sub
>
> --
> Rick (MVP - Excel)
>
>
> "SITCFanTN" <(E-Mail Removed)> wrote in message
> news:8542DAE9-E688-466D-9B8D-(E-Mail Removed)...
> > The date in my table is in mm/dd/yy format and located in column E. I
> > need
> > to delete all rows not the current date quickly. I want to build a macro
> > in
> > VB to delete all records not the current date. Any help you have is
> > greatly
> > appreciated, thank you.

>
> .
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      19th Nov 2009
The code was tested before I posted it and it worked fine. The only reason,
off the top of my head, that I can think of why it didn't work for you is
that your "dates" are not really Excel dates, rather, I think your dates
might be text representation of dates instead.

--
Rick (MVP - Excel)


"SITCFanTN" <(E-Mail Removed)> wrote in message
news:8E992DC9-5A9A-4A87-84CA-(E-Mail Removed)...
> HI Rick,
>
> This deleted all my records, it didn't just delete the records that were
> not
> dated for today. I only changed my sheet name...I changed your text of
> "sheet 7" to "All Records"
>
> Help, thank you.
>
> "Rick Rothstein" wrote:
>
>> Give the following macro a try; change my example setup in the three
>> Const
>> statements to match your actual layout. Note that I interpreted your
>> reference to "current date" to be today's date; if that was wrong, then
>> change the <>Date condition in the first If statement to the date value
>> you
>> actually want to use.
>>
>> Sub RemoveNotCurrentRecords()
>> Dim X As Long
>> Dim LastRow As Long
>> Dim OriginalCalculationMode As Long
>> Dim RowsToDelete As Range
>>
>> Const DataStartRow As Long = 1
>> Const UnionColumn As String = "E"
>> Const SheetName As String = "Sheet7"
>>
>> On Error GoTo Whoops
>> OriginalCalculationMode = Application.Calculation
>> Application.Calculation = xlCalculationManual
>> Application.ScreenUpdating = False
>>
>> With Worksheets(SheetName)
>> LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
>> For X = LastRow To DataStartRow Step -1
>> If .Cells(X, UnionColumn).Value <> Date Then
>> If RowsToDelete Is Nothing Then
>> Set RowsToDelete = .Cells(X, UnionColumn)
>> Else
>> Set RowsToDelete = Union(RowsToDelete, .Cells(X, UnionColumn))
>> End If
>> If RowsToDelete.Areas.Count > 100 Then
>> RowsToDelete.EntireRow.Delete xlShiftUp
>> Set RowsToDelete = Nothing
>> End If
>> End If
>> Next
>> End With
>> If Not RowsToDelete Is Nothing Then
>> RowsToDelete.EntireRow.Delete xlShiftUp
>> End If
>> Whoops:
>> Application.Calculation = OriginalCalculationMode
>> Application.ScreenUpdating = True
>> End Sub
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "SITCFanTN" <(E-Mail Removed)> wrote in message
>> news:8542DAE9-E688-466D-9B8D-(E-Mail Removed)...
>> > The date in my table is in mm/dd/yy format and located in column E. I
>> > need
>> > to delete all rows not the current date quickly. I want to build a
>> > macro
>> > in
>> > VB to delete all records not the current date. Any help you have is
>> > greatly
>> > appreciated, thank you.

>>
>> .
>>


 
Reply With Quote
 
SITCFanTN
Guest
Posts: n/a
 
      19th Nov 2009
That was it, once I converted to date format, this worked like a charm.
Thanks so much Rick!

"Rick Rothstein" wrote:

> The code was tested before I posted it and it worked fine. The only reason,
> off the top of my head, that I can think of why it didn't work for you is
> that your "dates" are not really Excel dates, rather, I think your dates
> might be text representation of dates instead.
>
> --
> Rick (MVP - Excel)
>
>
> "SITCFanTN" <(E-Mail Removed)> wrote in message
> news:8E992DC9-5A9A-4A87-84CA-(E-Mail Removed)...
> > HI Rick,
> >
> > This deleted all my records, it didn't just delete the records that were
> > not
> > dated for today. I only changed my sheet name...I changed your text of
> > "sheet 7" to "All Records"
> >
> > Help, thank you.
> >
> > "Rick Rothstein" wrote:
> >
> >> Give the following macro a try; change my example setup in the three
> >> Const
> >> statements to match your actual layout. Note that I interpreted your
> >> reference to "current date" to be today's date; if that was wrong, then
> >> change the <>Date condition in the first If statement to the date value
> >> you
> >> actually want to use.
> >>
> >> Sub RemoveNotCurrentRecords()
> >> Dim X As Long
> >> Dim LastRow As Long
> >> Dim OriginalCalculationMode As Long
> >> Dim RowsToDelete As Range
> >>
> >> Const DataStartRow As Long = 1
> >> Const UnionColumn As String = "E"
> >> Const SheetName As String = "Sheet7"
> >>
> >> On Error GoTo Whoops
> >> OriginalCalculationMode = Application.Calculation
> >> Application.Calculation = xlCalculationManual
> >> Application.ScreenUpdating = False
> >>
> >> With Worksheets(SheetName)
> >> LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
> >> For X = LastRow To DataStartRow Step -1
> >> If .Cells(X, UnionColumn).Value <> Date Then
> >> If RowsToDelete Is Nothing Then
> >> Set RowsToDelete = .Cells(X, UnionColumn)
> >> Else
> >> Set RowsToDelete = Union(RowsToDelete, .Cells(X, UnionColumn))
> >> End If
> >> If RowsToDelete.Areas.Count > 100 Then
> >> RowsToDelete.EntireRow.Delete xlShiftUp
> >> Set RowsToDelete = Nothing
> >> End If
> >> End If
> >> Next
> >> End With
> >> If Not RowsToDelete Is Nothing Then
> >> RowsToDelete.EntireRow.Delete xlShiftUp
> >> End If
> >> Whoops:
> >> Application.Calculation = OriginalCalculationMode
> >> Application.ScreenUpdating = True
> >> End Sub
> >>
> >> --
> >> Rick (MVP - Excel)
> >>
> >>
> >> "SITCFanTN" <(E-Mail Removed)> wrote in message
> >> news:8542DAE9-E688-466D-9B8D-(E-Mail Removed)...
> >> > The date in my table is in mm/dd/yy format and located in column E. I
> >> > need
> >> > to delete all rows not the current date quickly. I want to build a
> >> > macro
> >> > in
> >> > VB to delete all records not the current date. Any help you have is
> >> > greatly
> >> > appreciated, thank you.
> >>
> >> .
> >>

>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hide Rows (Current Date) / Unhide Rows =?Utf-8?B?Sm9lIEsu?= Microsoft Excel Programming 1 10th Oct 2007 05:37 PM
Delete Rows If Date Less Than Date Value =?Utf-8?B?Sm9lIEsu?= Microsoft Excel Programming 1 4th Oct 2007 10:50 PM
Update rows Printed column to current date/time upon report print =?Utf-8?B?SmltIEs=?= Microsoft Access VBA Modules 8 6th Aug 2007 06:41 PM
Deleting rows older than current date and inserting a new row =?Utf-8?B?RGFuIEUu?= Microsoft Excel Programming 4 14th Dec 2005 03:16 PM
Excel VBA - Delete rows within a date range rott Microsoft Excel Programming 9 25th Feb 2004 02:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:55 PM.