PC Review


Reply
Thread Tools Rate Thread

Date ranges in macros

 
 
Zak
Guest
Posts: n/a
 
      13th Jan 2008
Hi, i asked this question in a previous thread but it still hasnt been
answered and a couple of my other questions have so was wondering whether i
need to re-submit.

i want to write a macro that will recognise that the date in column x is
beyond
todays date and then delete the rows.

So, if the date in column X is beyond/greater than todays date (and this date
should be the current day) then the rows should be deleted.

thanks.
 
Reply With Quote
 
 
 
 
Lazzzx
Guest
Posts: n/a
 
      13th Jan 2008
Hi Zak,

This routine should do your job. The dates to test are in column x:

Sub DeleteRowsbyDate()
For Each cll In Range([x1], [x1].End(xlDown))
If IsDate(cll) And cll > Now() Then _
cll.EntireRow.Delete
Next
End Sub

CAUTION!! Use with care... you cannot undo these changes.

regards,
Lazzzx


"Zak" <(E-Mail Removed)> skrev i meddelelsen
news:7F31C1C1-BCAC-4F64-802C-(E-Mail Removed)...
> Hi, i asked this question in a previous thread but it still hasnt been
> answered and a couple of my other questions have so was wondering whether
> i
> need to re-submit.
>
> i want to write a macro that will recognise that the date in column x is
> beyond
> todays date and then delete the rows.
>
> So, if the date in column X is beyond/greater than todays date (and this
> date
> should be the current day) then the rows should be deleted.
>
> thanks.


 
Reply With Quote
 
FSt1
Guest
Posts: n/a
 
      13th Jan 2008
hi
I think your have left out a lot for someone to give a good reply but i will
make some guesses.
assume that you are in a loop, with the key being column x.....
If activecell.Value > Date Then
activecell.EntireRow.Delete shift:=xlUp
End If

Personally i don't like using activecell in a loop. it requires select which
slows things down. i prefer using variables. something like this....
Sub test1()
Dim r As Range
Dim rd As Range
Set r = Range("X2") 'assume a header
Do While Not IsEmpty(r)
Set rd = r.Offset(1, 0)
If r.Value > Date Then
r.EntireRow.Delete shift:=xlUp
End If
Set r = rd 'move down one or next
Loop
End Sub

I'm using a do loop here because i'm dealing with an unknow ie(how may rows)
most programmers perfer the for next loop but each has it's purpose. that's
why vb has both. do loops can get out of control if not coded right ie run
away code. for next loop only do thing a certain number of times then quits
but you must find the limits to restrict it to.
for next loop
Sub test2()
Dim r As Range

lr = Cells(Rows.Count, "X").End(xlUp).Row 'finds the last row in data
Set r = Range("x2:x" & lr)

For Each cell In r
If cell.Value > Date Then
cell.EntireRow.Delete
End If
Next cell

End Sub

hope this wild guessing is giving you ideas
regards
FSt1

"Zak" wrote:

> Hi, i asked this question in a previous thread but it still hasnt been
> answered and a couple of my other questions have so was wondering whether i
> need to re-submit.
>
> i want to write a macro that will recognise that the date in column x is
> beyond
> todays date and then delete the rows.
>
> So, if the date in column X is beyond/greater than todays date (and this date
> should be the current day) then the rows should be deleted.
>
> thanks.

 
Reply With Quote
 
Zak
Guest
Posts: n/a
 
      14th Jan 2008
Thanks for all your guesses. Each one worked but the problem is when i ran
the macro i saw that it only deleted a few lines at a time. I had to run the
macro 3 times (clicked it 3 times) to get it to delete all the rows i wanted!

why is this happening? cant it delete everything with one click?

thanks.

"FSt1" wrote:

> hi
> I think your have left out a lot for someone to give a good reply but i will
> make some guesses.
> assume that you are in a loop, with the key being column x.....
> If activecell.Value > Date Then
> activecell.EntireRow.Delete shift:=xlUp
> End If
>
> Personally i don't like using activecell in a loop. it requires select which
> slows things down. i prefer using variables. something like this....
> Sub test1()
> Dim r As Range
> Dim rd As Range
> Set r = Range("X2") 'assume a header
> Do While Not IsEmpty(r)
> Set rd = r.Offset(1, 0)
> If r.Value > Date Then
> r.EntireRow.Delete shift:=xlUp
> End If
> Set r = rd 'move down one or next
> Loop
> End Sub
>
> I'm using a do loop here because i'm dealing with an unknow ie(how may rows)
> most programmers perfer the for next loop but each has it's purpose. that's
> why vb has both. do loops can get out of control if not coded right ie run
> away code. for next loop only do thing a certain number of times then quits
> but you must find the limits to restrict it to.
> for next loop
> Sub test2()
> Dim r As Range
>
> lr = Cells(Rows.Count, "X").End(xlUp).Row 'finds the last row in data
> Set r = Range("x2:x" & lr)
>
> For Each cell In r
> If cell.Value > Date Then
> cell.EntireRow.Delete
> End If
> Next cell
>
> End Sub
>
> hope this wild guessing is giving you ideas
> regards
> FSt1
>
> "Zak" wrote:
>
> > Hi, i asked this question in a previous thread but it still hasnt been
> > answered and a couple of my other questions have so was wondering whether i
> > need to re-submit.
> >
> > i want to write a macro that will recognise that the date in column x is
> > beyond
> > todays date and then delete the rows.
> >
> > So, if the date in column X is beyond/greater than todays date (and this date
> > should be the current day) then the rows should be deleted.
> >
> > thanks.

 
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
Expanding ranges in macros Jeff Microsoft Excel Misc 4 5th Jan 2009 10:16 PM
Selecting ranges in macros =?Utf-8?B?Y2FybA==?= Microsoft Excel Programming 1 17th Jul 2007 05:04 PM
Defining Ranges using macros bhussey Microsoft Excel Programming 5 18th May 2006 11:26 PM
Named ranges in macros? fverlaine Microsoft Excel Misc 3 14th Dec 2005 05:34 PM
Re: Dynamic ranges in macros Jeff Microsoft Excel Worksheet Functions 0 30th Jun 2003 07:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:26 PM.