Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Delete Rows Prior to Last Month

Hi
This macro should delete rows from an imported txt for all entries that are
prior to last month. It is not doing this. How does the macro know what the
current date is in order to keep all entries for the month prior, and to
delete all other entries.

I know zero about VBA and have copied the macro below.


Delete rows prior to last month
Sub DeleteRowsPriorLastMonth()
Dim LResult As String
Range("U1").Select ' may have to revert to T2
Do While Trim(ActiveCell.Value) < ""
Application.StatusBar = "Deleting SIFT rows where TXN_DATE older than
last month - count: " & g_lngDeletedRowCount
'job 1 - Replace dots with dashes in the TXN_DATE column
LResult = Replace(ActiveCell.Offset(1, 0).Value, ".", "/")
ActiveCell.Offset(1, 0).Value = Trim(LResult)
' Delete row
If Month(ActiveCell.Offset(1, 0).Value) < Month(Date) - 1 Then
ActiveCell.Offset(1, 0).EntireRow.Delete
g_lngDeletedRowCount = g_lngDeletedRowCount + 1 'count deleted
row
Else: ActiveCell.Offset(1, 0).Select 'count row not deleted
g_lngRowCount = g_lngRowCount + 1
End If
Loop
End Sub

Regards
Alison
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default Delete Rows Prior to Last Month

Alison

The problem with the code is in this line:-
If Month(ActiveCell.Offset(1, 0).Value) < Month(Date) - 1 Then
This code will not work (for example) in February. You are wanting to
delete December transactions, but as month 12 is not less than month 1,
nothing gets deleted. As Shazur has pointed out you need to use the DateDiff
function to work out the number of months inbetween now and the transaction
date.

It's probably also worth looking at the lines of code which replace the dots
with slashes. I would think that these are going to cause problems, probably
changing what were UK dates to US.
--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.


"Alison" wrote:

Hi
This macro should delete rows from an imported txt for all entries that are
prior to last month. It is not doing this. How does the macro know what the
current date is in order to keep all entries for the month prior, and to
delete all other entries.

I know zero about VBA and have copied the macro below.


Delete rows prior to last month
Sub DeleteRowsPriorLastMonth()
Dim LResult As String
Range("U1").Select ' may have to revert to T2
Do While Trim(ActiveCell.Value) < ""
Application.StatusBar = "Deleting SIFT rows where TXN_DATE older than
last month - count: " & g_lngDeletedRowCount
'job 1 - Replace dots with dashes in the TXN_DATE column
LResult = Replace(ActiveCell.Offset(1, 0).Value, ".", "/")
ActiveCell.Offset(1, 0).Value = Trim(LResult)
' Delete row
If Month(ActiveCell.Offset(1, 0).Value) < Month(Date) - 1 Then
ActiveCell.Offset(1, 0).EntireRow.Delete
g_lngDeletedRowCount = g_lngDeletedRowCount + 1 'count deleted
row
Else: ActiveCell.Offset(1, 0).Select 'count row not deleted
g_lngRowCount = g_lngRowCount + 1
End If
Loop
End Sub

Regards
Alison

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
15th of prior month Deb Excel Worksheet Functions 3 February 6th 07 09:48 PM
dates, 1 month prior wfactor Excel Worksheet Functions 5 June 19th 06 08:23 AM
formula for month(s) prior to actual braadi Excel Worksheet Functions 4 December 7th 05 05:00 PM
formula for month that is prior to actual braadi Excel Worksheet Functions 4 December 6th 05 07:32 PM
Prior Month Howard Excel Discussion (Misc queries) 6 November 2nd 05 03:06 PM


All times are GMT +1. The time now is 10:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"