Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default conditions are met but actions not (always) taken

I've got a database sheet of about 3000 rows with a date column (values from
2004 up to yesterday).
When I run this:
If cell.Value < (CDate(Now) - 2) Then cell.Interior.ColorIndex = 6
all the cells up to three days ago turn yellow, as they should.

But when i run this:
If cell.Value < (CDate(Now) - 2) Then EntireRow.Delete
about half the rows are deleted but I still have hundreds of dates from
years ago.
I've tried splitting the "If" into:
If cell.Value < (CDate(Now) - 2) Then
EntireRow.Select
Selection.Delete
End if
but the result is the same.

Can anyone explain why I'm getting different results and how this apparently
random selection is taking place?

--
WinXP - Office2003 (Italian)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default conditions are met but actions not (always) taken

It will be something to do with your loop around the rows, because when you
do an EntireRow.Delete then rows below will move up. Are you remaining on the
current row after a delete ? or moving 1 row down which would miss the row
below that has just moved up ?

"David Macdonald" wrote:

I've got a database sheet of about 3000 rows with a date column (values from
2004 up to yesterday).
When I run this:
If cell.Value < (CDate(Now) - 2) Then cell.Interior.ColorIndex = 6
all the cells up to three days ago turn yellow, as they should.

But when i run this:
If cell.Value < (CDate(Now) - 2) Then EntireRow.Delete
about half the rows are deleted but I still have hundreds of dates from
years ago.
I've tried splitting the "If" into:
If cell.Value < (CDate(Now) - 2) Then
EntireRow.Select
Selection.Delete
End if
but the result is the same.

Can anyone explain why I'm getting different results and how this apparently
random selection is taking place?

--
WinXP - Office2003 (Italian)

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default conditions are met but actions not (always) taken

It sounds like you need to reverse your loop. Plus it looks like you are
using a For Each...Next Loop, right? You can only reverse a loop with the
For...Next Loop. I wrote some code that should help. I assumed your dates
you want to scan are in Col.A. This code will find the last cell with data
in it in Col A, scan all cells up to the header row (row 1) and delete the
entire row if the If...Then statement is True. Hope this helps! If so, let
me know, click "YES" below.

Sub DeleteRows()

Dim i As Long
Dim LastRow As Long

' finds last row in column A
LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

For i = LastRow To 2 Step -1
If Cells(i, "A").Value < (CDate(Now) - 2) Then Rows(i).Delete
Next i

End Sub

--
Cheers,
Ryan


"David Macdonald" wrote:

I've got a database sheet of about 3000 rows with a date column (values from
2004 up to yesterday).
When I run this:
If cell.Value < (CDate(Now) - 2) Then cell.Interior.ColorIndex = 6
all the cells up to three days ago turn yellow, as they should.

But when i run this:
If cell.Value < (CDate(Now) - 2) Then EntireRow.Delete
about half the rows are deleted but I still have hundreds of dates from
years ago.
I've tried splitting the "If" into:
If cell.Value < (CDate(Now) - 2) Then
EntireRow.Select
Selection.Delete
End if
but the result is the same.

Can anyone explain why I'm getting different results and how this apparently
random selection is taking place?

--
WinXP - Office2003 (Italian)

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default conditions are met but actions not (always) taken

Divide the prize money equally between the three of you!
--
WinXP - Office2003 (Italian)


"David Macdonald" wrote:

I've got a database sheet of about 3000 rows with a date column (values from
2004 up to yesterday).
When I run this:
If cell.Value < (CDate(Now) - 2) Then cell.Interior.ColorIndex = 6
all the cells up to three days ago turn yellow, as they should.

But when i run this:
If cell.Value < (CDate(Now) - 2) Then EntireRow.Delete
about half the rows are deleted but I still have hundreds of dates from
years ago.
I've tried splitting the "If" into:
If cell.Value < (CDate(Now) - 2) Then
EntireRow.Select
Selection.Delete
End if
but the result is the same.

Can anyone explain why I'm getting different results and how this apparently
random selection is taking place?

--
WinXP - Office2003 (Italian)

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
Record Actions Martin Excel Programming 0 December 24th 08 12:09 PM
Form Actions Phil Excel Programming 7 February 14th 08 06:55 PM
if - then actions N+ Excel Programming 5 February 10th 08 03:38 PM
Capture All Actions 2 vqthomf Excel Programming 1 October 4th 06 01:21 PM
Actions between user actions Indiana Epilepsy and Child Neurology[_2_] Excel Programming 5 August 23rd 06 09:22 PM


All times are GMT +1. The time now is 01:18 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"