PC Review


Reply
Thread Tools Rate Thread

Delete non weekdays

 
 
Arne Hegefors
Guest
Posts: n/a
 
      7th Dec 2007
Hi! I have long lists of dates that I often have to clean by deleting all
dates that are weekend dates. I am trying to write a sub that does this for
me (i know how to do it in a worksheet but it takes too long time since i
have very many books..). I tried selction the dates that I want to clean and
then running the macro but that obviously does not work. Is it posiible to do
this in any way? any help appreciated!

Sub deleteWeekEnds()
Dim i As Integer
i = Weekday(Selection, vbMonday)
If i = 6 Or i = 7 Then
Selection.Row.Delete
End If
End Sub
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      7th Dec 2007
Sub arne()
Dim d As Date
d = Selection.Value
n = Application.WorksheetFunction.Weekday(d)
If n = 7 Or n = 1 Then
Selection.EntireRow.Delete
End If
End Sub


and remember that if this is in a loop, run the loop backwards
--
Gary''s Student - gsnu200760


"Arne Hegefors" wrote:

> Hi! I have long lists of dates that I often have to clean by deleting all
> dates that are weekend dates. I am trying to write a sub that does this for
> me (i know how to do it in a worksheet but it takes too long time since i
> have very many books..). I tried selction the dates that I want to clean and
> then running the macro but that obviously does not work. Is it posiible to do
> this in any way? any help appreciated!
>
> Sub deleteWeekEnds()
> Dim i As Integer
> i = Weekday(Selection, vbMonday)
> If i = 6 Or i = 7 Then
> Selection.Row.Delete
> End If
> End Sub

 
Reply With Quote
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      7th Dec 2007
On Dec 7, 10:28 am, Arne Hegefors
<ArneHegef...@discussions.microsoft.com> wrote:
> Hi! I have long lists of dates that I often have to clean by deleting all
> dates that are weekend dates. I am trying to write a sub that does this for
> me (i know how to do it in a worksheet but it takes too long time since i
> have very many books..). I tried selction the dates that I want to clean and
> then running the macro but that obviously does not work. Is it posiible to do
> this in any way? any help appreciated!
>
> Sub deleteWeekEnds()
> Dim i As Integer
> i = Weekday(Selection, vbMonday)
> If i = 6 Or i = 7 Then
> Selection.Row.Delete
> End If
> End Sub


Hi
Try this:

Sub deleteWeekEnds()
Dim i As Integer, DateCell As Range
For Each DateCell In Selection
i = Weekday(DateCell, vbSaturday)
If (i = 0 Or i = 1) Then
DateCell.EntireRow.Delete
End If
Next DateCell
End Sub

regards
Paul

 
Reply With Quote
 
Arne Hegefors
Guest
Posts: n/a
 
      7th Dec 2007
Thanks alot for your answer! However the code works only if just one cell is
selected. If many cells are selected it does not work. Is it posiible to fix
this? Thanks!

"Gary''s Student" skrev:

> Sub arne()
> Dim d As Date
> d = Selection.Value
> n = Application.WorksheetFunction.Weekday(d)
> If n = 7 Or n = 1 Then
> Selection.EntireRow.Delete
> End If
> End Sub
>
>
> and remember that if this is in a loop, run the loop backwards
> --
> Gary''s Student - gsnu200760
>
>
> "Arne Hegefors" wrote:
>
> > Hi! I have long lists of dates that I often have to clean by deleting all
> > dates that are weekend dates. I am trying to write a sub that does this for
> > me (i know how to do it in a worksheet but it takes too long time since i
> > have very many books..). I tried selction the dates that I want to clean and
> > then running the macro but that obviously does not work. Is it posiible to do
> > this in any way? any help appreciated!
> >
> > Sub deleteWeekEnds()
> > Dim i As Integer
> > i = Weekday(Selection, vbMonday)
> > If i = 6 Or i = 7 Then
> > Selection.Row.Delete
> > End If
> > End Sub

 
Reply With Quote
 
Gary''s Student
Guest
Posts: n/a
 
      7th Dec 2007
We will build a Union and then do only one delete row:

Sub arne2()
Dim d As Date
Set rdel = Nothing
For Each r In Selection
d = r.Value
n = Application.WorksheetFunction.Weekday(d)
If n = 7 Or n = 1 Then
If rdel Is Nothing Then
Set rdel = r
Else
Set rdel = Union(rdel, r)
End If
End If
Next

If rdel Is Nothing Then
Else
rdel.EntireRow.Delete
End If
End Sub

--
Gary''s Student - gsnu200760


"Arne Hegefors" wrote:

> Thanks alot for your answer! However the code works only if just one cell is
> selected. If many cells are selected it does not work. Is it posiible to fix
> this? Thanks!
>
> "Gary''s Student" skrev:
>
> > Sub arne()
> > Dim d As Date
> > d = Selection.Value
> > n = Application.WorksheetFunction.Weekday(d)
> > If n = 7 Or n = 1 Then
> > Selection.EntireRow.Delete
> > End If
> > End Sub
> >
> >
> > and remember that if this is in a loop, run the loop backwards
> > --
> > Gary''s Student - gsnu200760
> >
> >
> > "Arne Hegefors" wrote:
> >
> > > Hi! I have long lists of dates that I often have to clean by deleting all
> > > dates that are weekend dates. I am trying to write a sub that does this for
> > > me (i know how to do it in a worksheet but it takes too long time since i
> > > have very many books..). I tried selction the dates that I want to clean and
> > > then running the macro but that obviously does not work. Is it posiible to do
> > > this in any way? any help appreciated!
> > >
> > > Sub deleteWeekEnds()
> > > Dim i As Integer
> > > i = Weekday(Selection, vbMonday)
> > > If i = 6 Or i = 7 Then
> > > Selection.Row.Delete
> > > End If
> > > End Sub

 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      7th Dec 2007
On Fri, 7 Dec 2007 02:28:02 -0800, Arne Hegefors
<(E-Mail Removed)> wrote:

>Hi! I have long lists of dates that I often have to clean by deleting all
>dates that are weekend dates. I am trying to write a sub that does this for
>me (i know how to do it in a worksheet but it takes too long time since i
>have very many books..). I tried selction the dates that I want to clean and
>then running the macro but that obviously does not work. Is it posiible to do
>this in any way? any help appreciated!
>
>Sub deleteWeekEnds()
> Dim i As Integer
> i = Weekday(Selection, vbMonday)
> If i = 6 Or i = 7 Then
> Selection.Row.Delete
> End If
>End Sub


What you need to do is select the bottom row in your range, and then step
upwards, testing and deleting as you go.

I'm not sure what the most efficient way in your worksheet is to select the
bottom row, so I just used column A, but you can change this in many ways.

Here's an example:

=======================================
Option Explicit
Sub DelWeekends()
Dim LastRow As Range, c As Range
Dim i As Long
'you should change this to reflect the appropriate column
Set LastRow = Range("a65535").End(xlUp)

For i = LastRow.Row To 1 Step -1
Set c = Cells(i, 1)
If IsDate(c) Then
If Weekday(c) = vbSaturday Or Weekday(c) = vbSunday Then
c.EntireRow.Delete
End If
End If
Next i
End Sub
====================================
--ron
 
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
just weekdays peter the swede Microsoft Excel Programming 9 4th Sep 2006 09:51 PM
Weekdays =?Utf-8?B?SmVmZg==?= Microsoft Excel Misc 7 15th Feb 2005 12:18 AM
weekdays =?Utf-8?B?UmVkQ2hlcXVlcg==?= Microsoft Excel Misc 13 2nd Oct 2004 04:16 AM
weekdays Tom Microsoft Excel Discussion 5 12th Mar 2004 01:42 PM
Ask for weekdays only Beth Microsoft Access Queries 1 9th Mar 2004 04:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:09 AM.