#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default Date Entry

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Date Entry

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default Date Entry

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Date Entry

On Tue, 9 Sep 2008 02:58:00 -0700, 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


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
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Date Entry

On Tue, 09 Sep 2008 07:24:15 -0400, Ron Rosenfeld
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
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
Autofill date on date entry in a separate column lemonshark Excel Worksheet Functions 2 February 18th 10 05:11 PM
date entry using . not / Klonger Excel Discussion (Misc queries) 1 August 26th 09 10:27 AM
Excel 2002 date entry: Cannot get away from d-m-yy entry format Steve Eklund Excel Discussion (Misc queries) 3 May 11th 09 04:57 PM
Restricting date entry relative to current date bwalshjr Excel Worksheet Functions 1 April 10th 07 12:49 PM
Date entry magstate Excel Discussion (Misc queries) 2 March 23rd 05 07:15 PM


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