PC Review


Reply
 
 
Richard
Guest
Posts: n/a
 
      9th Sep 2008
I have 2 drop down lists. 1 so that I can select the day ie Sunday the other
so that I can select a period ie Year 2009.

Would it be possible to have a macro create a list of all the dates which
match the criteria ie all Sundays in 2009.

Thanks in advance
Richard
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      9th Sep 2008
Try this code. I hard coded the year and day of week. Change as required.
the code puts the dates in column A


Sub MakeDates()

MyYear = 2009 'Change to Range("B1") or equivalent
DayofWeek = "Tuesday" 'Change to Range("C1") or equivalent.

DayCount = 0
Do
DayCount = DayCount + 1
DayName = WeekdayName(DayCount, False, vbSunday)
Loop While DayofWeek <> DayName

FirstDay = DateValue("1/1/" & MyYear)
LastDay = DateValue("12/31/" & MyYear)

'Get First Day of year that matches DayofWeek
DayOffset = DayCount - Weekday(FirstDay)
If DayOffset < 0 Then DayOffset = DayOffset + 7
FirstDay = FirstDay + DayOffset

RowCount = 1
For DayCount = FirstDay To LastDay Step 7
Range("A" & RowCount) = Format(DayCount, "MMMM DD, YYYY")
RowCount = RowCount + 1
Next DayCount
End Sub

"Richard" wrote:

> I have 2 drop down lists. 1 so that I can select the day ie Sunday the other
> so that I can select a period ie Year 2009.
>
> Would it be possible to have a macro create a list of all the dates which
> match the criteria ie all Sundays in 2009.
>
> Thanks in advance
> Richard

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      9th Sep 2008
Thanks I'll give it a go

"Joel" wrote:

> Try this code. I hard coded the year and day of week. Change as required.
> the code puts the dates in column A
>
>
> Sub MakeDates()
>
> MyYear = 2009 'Change to Range("B1") or equivalent
> DayofWeek = "Tuesday" 'Change to Range("C1") or equivalent.
>
> DayCount = 0
> Do
> DayCount = DayCount + 1
> DayName = WeekdayName(DayCount, False, vbSunday)
> Loop While DayofWeek <> DayName
>
> FirstDay = DateValue("1/1/" & MyYear)
> LastDay = DateValue("12/31/" & MyYear)
>
> 'Get First Day of year that matches DayofWeek
> DayOffset = DayCount - Weekday(FirstDay)
> If DayOffset < 0 Then DayOffset = DayOffset + 7
> FirstDay = FirstDay + DayOffset
>
> RowCount = 1
> For DayCount = FirstDay To LastDay Step 7
> Range("A" & RowCount) = Format(DayCount, "MMMM DD, YYYY")
> RowCount = RowCount + 1
> Next DayCount
> End Sub
>
> "Richard" wrote:
>
> > I have 2 drop down lists. 1 so that I can select the day ie Sunday the other
> > so that I can select a period ie Year 2009.
> >
> > Would it be possible to have a macro create a list of all the dates which
> > match the criteria ie all Sundays in 2009.
> >
> > Thanks in advance
> > Richard

 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      9th Sep 2008
On Tue, 9 Sep 2008 02:58:00 -0700, Richard <(E-Mail Removed)>
wrote:

>I have 2 drop down lists. 1 so that I can select the day ie Sunday the other
>so that I can select a period ie Year 2009.
>
>Would it be possible to have a macro create a list of all the dates which
>match the criteria ie all Sundays in 2009.
>
>Thanks in advance
>Richard


This was written as a worksheet change macro, so the change in the list would
occur whenever you changed the inputs.

To enter this, right click on the sheet tab; select View Code; and paste the
code below into the window that opens:

===================================
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rInputs As Range
Dim rOutput As Range
Dim lWeekday As Long
Dim lYear As Long
Dim lFirstDay As Long
Dim i As Long

Set rInputs = Range("A1:A2")
Set rOutput = Range("B1:B53")

'Change made in A1 or A2?
If Not Intersect(Target, rInputs) Is Nothing And _
WorksheetFunction.CountA(rInputs) = 2 Then
rOutput.Clear
rOutput.NumberFormat = "dddd, mmmm dd, yyyy"
With Range("A1")
'Assumes weekday list in order Sunday, Monday ... Saturday
lWeekday = WorksheetFunction.Match(.Value,
Range(Mid(.Validation.Formula1, 2)), 0)
End With
lYear = Range("A2").Value
lFirstDay = DateSerial(lYear, 1, 8) - _
Weekday(DateSerial(lYear, 1, 8 - lWeekday))
[b2].Value = lFirstDay
End If

i = 0
Do Until Year(lFirstDay + 7 * i) <> lYear
rOutput(i + 1, 1) = lFirstDay + 7 * i
i = i + 1
Loop

End Sub
===========================
--ron
 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      9th Sep 2008
On Tue, 09 Sep 2008 07:24:15 -0400, Ron Rosenfeld <(E-Mail Removed)>
wrote:

>lWeekday = WorksheetFunction.Match(.Value,
>Range(Mid(.Validation.Formula1, 2)), 0)


The above line may be word-wrapped inappropriately on your reader (it is on
mine). It should be all one line.
--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
How do I time/date stamp an entry in a note from a calendar entry? Sweet Microsoft Outlook Calendar 1 2nd Apr 2010 07:33 PM
Excel 2002 date entry: Cannot get away from d-m-yy entry format Steve Eklund Microsoft Excel Misc 3 11th May 2009 04:57 PM
i have two date fileds Opend date Due date, can i set default on due date so, its always = to open date on my data entry form1 Urgent Mike Saifie Microsoft Access Form Coding 1 9th Mar 2006 01:08 AM
Data Entry Restrictions & Date/Time Entry =?Utf-8?B?S01jR2Vl?= Microsoft Excel Misc 3 30th Dec 2003 05:58 PM
Date fields automatically changing when today date matches date field entry.Help! Brian Cassin Microsoft Access Forms 1 15th Nov 2003 01:51 PM


Features
 

Advertising
 

Newsgroups
 


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