PC Review


Reply
Thread Tools Rate Thread

delete rows in excel by service date

 
 
jeff.white@amcore.com
Guest
Posts: n/a
 
      29th May 2007
I have an excel sheet that contains about 300 rows of data. One
column, P, has the hire date. What I'm looking to do via vba is to
delete all rows where the hire date is not 3 months prior next month.
In other words, next month is June, so I want only those rows where
the hire date is the month of March (3 months prior to June)...I
normally use the filter commands which works OK, but I thought since
this is a monthly report I can speed things up and run it via VBA.

Any suggestions?

 
Reply With Quote
 
 
 
 
ra
Guest
Posts: n/a
 
      29th May 2007
Try this, it will allow you to select date range to delete.. (update
column # as appropriate -this assumes column 13)

Sub DeleteDatesRIM()
Dim StDate As Date, FinDate As Date, LastRow&, i&
StDate = InputBox("Dates to be Deleted- ENTER Start Date (DD/MM/
YYYY)")
FinDate = InputBox("Dates to be Deleted-ENTER End Date (DD/MM/
YYYY)")
LastRow = Cells(Rows.Count, 13).End(xlUp).Row
For i = LastRow To 2 Step -1
If Cells(i, 13).Value >= StDate And _
Cells(i, 13).Value <= FinDate Then
Rows(i).Delete
End If
Next i
End Sub

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      29th May 2007
Turn on the macro recorder while you apply the filter.

Turn off the macro recorder and use the recorded code as model code to build
your macro.

--
Regards,
Tom Ogilvy


"(E-Mail Removed)" wrote:

> I have an excel sheet that contains about 300 rows of data. One
> column, P, has the hire date. What I'm looking to do via vba is to
> delete all rows where the hire date is not 3 months prior next month.
> In other words, next month is June, so I want only those rows where
> the hire date is the month of March (3 months prior to June)...I
> normally use the filter commands which works OK, but I thought since
> this is a monthly report I can speed things up and run it via VBA.
>
> Any suggestions?
>
>

 
Reply With Quote
 
Nigel
Guest
Posts: n/a
 
      29th May 2007
One approach would be to scan the rows from last to first, checking if
column P has a valid date (will not remove titles etc.)
The code determines the last row from the data in column P. Adjustments are
made for dates that span a year rollover

Sub RemoveRows()
Dim xr As Long, xlr As Long, mth As Integer
Application.ScreenUpdating = False
with Sheets("Sheet1")
xlr = .Cells(.Rows.Count, "P").End(xlUp).Row
mth = Month(Date)
If mth < 3 Then mth = mth + 12
For xr = xlr To 1 Step -1
If IsDate(.Cells(xr, "P")) Then
If mth <> Month(.Cells(xr, "P")) + 2 Then
.Rows(xr).EntireRow.Delete shift:=xlUp
End If
End If
Next
End With
Application.ScreenUpdating = True
End Sub

--
Cheers
Nigel



<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have an excel sheet that contains about 300 rows of data. One
> column, P, has the hire date. What I'm looking to do via vba is to
> delete all rows where the hire date is not 3 months prior next month.
> In other words, next month is June, so I want only those rows where
> the hire date is the month of March (3 months prior to June)...I
> normally use the filter commands which works OK, but I thought since
> this is a monthly report I can speed things up and run it via VBA.
>
> Any suggestions?
>



 
Reply With Quote
 
jeff.white@amcore.com
Guest
Posts: n/a
 
      29th May 2007
On May 29, 11:43 am, "Nigel" <nigel...@suxnospampanet.com> wrote:
> One approach would be to scan the rows from last to first, checking if
> column P has a valid date (will not remove titles etc.)
> The code determines the last row from the data in column P. Adjustments are
> made for dates that span a year rollover
>
> Sub RemoveRows()
> Dim xr As Long, xlr As Long, mth As Integer
> Application.ScreenUpdating = False
> with Sheets("Sheet1")
> xlr = .Cells(.Rows.Count, "P").End(xlUp).Row
> mth = Month(Date)
> If mth < 3 Then mth = mth + 12
> For xr = xlr To 1 Step -1
> If IsDate(.Cells(xr, "P")) Then
> If mth <> Month(.Cells(xr, "P")) + 2 Then
> .Rows(xr).EntireRow.Delete shift:=xlUp
> End If
> End If
> Next
> End With
> Application.ScreenUpdating = True
> End Sub
>
> --
> Cheers
> Nigel
>
> <jeff.wh...@amcore.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> >I have an excel sheet that contains about 300 rows of data. One
> > column, P, has the hire date. What I'm looking to do via vba is to
> > delete all rows where the hire date is not 3 months prior next month.
> > In other words, next month is June, so I want only those rows where
> > the hire date is the month of March (3 months prior to June)...I
> > normally use the filter commands which works OK, but I thought since
> > this is a monthly report I can speed things up and run it via VBA.

>
> > Any suggestions?- Hide quoted text -

>
> - Show quoted text -


Thanks to all that responded! I've was able to incorp the first
suggestion, I made a couple of modifications. Bascially I used stdate
is <= mm/dd/yy OR findate >= mm/dd/yy. This leaves the target month
in the sheet, deleting all those greater or less than....Thanks
again...

 
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
Delete all rows not current date SITCFanTN Microsoft Excel Programming 6 19th Nov 2009 01:38 PM
Macro to delete rows based on date PMBO Microsoft Excel Misc 4 18th Feb 2009 01:50 PM
Delete Rows If Date Less Than Date Value =?Utf-8?B?Sm9lIEsu?= Microsoft Excel Programming 1 4th Oct 2007 10:50 PM
How to Delete empty rows in excel in b/w rows with values =?Utf-8?B?RGVubmlz?= Microsoft Excel Worksheet Functions 3 28th Aug 2007 04:15 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 07:11 PM.