Weekday Function

B

Bo Hansson

I have experimented with the Weekday Function to find out what weekday
introduces every new year ahead.
Using my systems short date format (yyyy-mm-dd) I've typed different dates
into the code below, but the function
does not return the correct values. Furthermore, in my final solution I have
to include yyyy as a variable.

My test code: MsgBox Weekday(2006-01-01)

Please HELP!

/BosseH
 
R

Roger H. Levy

You need to enclose that form of a date in quotes, e.g.
Weekday("2006-01-01")

RL
 
H

Helmut Weber

Hi Bo,

have a very close look at this:

MsgBox Format(Weekday("01/01/2006"), "ddd")

Tested with an international version (US).

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tony Jollans

To enter date literals in VBA you surround them with hash symbols (#) ...

MsgBox Weekday(#2006-01-01#)

When you do this VBA will automatically change the date format to
US-standard, giving ..

MsgBox Weekday(#1/1/2006#)

VBA is also clever enough to interpret string literals, so this should also
work ...

MsgBox Weekday("2006-01-01")

BUT ... what you emtered -- 2006-01-01 -- without any delimiters -- is
treated as numeric (i.e. 2006 - 1 - 1 = 2004). The 2004 is then treated as
an internal date format (the 2004th day since 31/12/1899) and you get
whatever day of the week that was.
 
G

Greg

Try something like:
Sub Test()
Dim myDate
Dim myDayOfWeek
Dim myYear As String
Retry:
myYear = InputBox("Enter the four digit year: ")
If Len(myYear) <> 4 Or Not IsNumeric(myYear) Then GoTo Retry
myDate = "1/1/" & myYear
myDayOfWeek = Weekday(myDate)
Select Case myDayOfWeek
Case Is = 1
MsgBox "Sunday"
Case Is = 2
MsgBox "Monday"
Case Is = 3
MsgBox "Tuesday"
Case Is = 4
MsgBox "Wednesday"
Case Is = 5
MsgBox "Thursday"
Case Is = 6
MsgBox "Friday"
Case Is = 7
MsgBox "Saturday"
Case Else
'Do Nothing
End Select
End Sub
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
Try something like:
Sub Test()
Dim myDate
Dim myDayOfWeek
Dim myYear As String
Retry:
myYear = InputBox("Enter the four digit year: ")
If Len(myYear) <> 4 Or Not IsNumeric(myYear) Then GoTo Retry
myDate = "1/1/" & myYear
myDayOfWeek = Weekday(myDate)
Select Case myDayOfWeek
Case Is = 1
MsgBox "Sunday"
Case Is = 2
MsgBox "Monday"
Case Is = 3
MsgBox "Tuesday"
Case Is = 4
MsgBox "Wednesday"
Case Is = 5
MsgBox "Thursday"
Case Is = 6
MsgBox "Friday"
Case Is = 7
MsgBox "Saturday"
Case Else
'Do Nothing
End Select
End Sub

Just in case you had not seen this alternative way of checking user input
for errors...
If you have a long piece of code, the GoTo statements may cause you to pull
out your hair when debugging... Of course, in this case, since the code is
so simple, it is not a problem, but as a matter of habit, I never use GoTo
in such cases. I use this instead:

Do
myYear = InputBox("Enter the four digit year: ")
Loop While Len(myYear) <> 4 Or Not IsNumeric(myYear)

Or, if you want to allow the user a chance to cancel out of the loop:

Do
myYear = InputBox("Enter the four digit year: ")
If myYear = "" Then Exit Sub
Loop While Len(myYear) <> 4 Or Not IsNumeric(myYear)

So, using Helmut's code, the code would be:

'_______________________________________
Dim myDate As String
Dim myYear As String

Do
myYear = InputBox("Enter the four digit year: ")
If myYear = "" Then Exit Sub
Loop While Len(myYear) <> 4 Or Not IsNumeric(myYear)

myDate = "1/1/" & myYear

MsgBox Format(Weekday(myDate), "dddd")
'_______________________________________

As the saying goes, there are many ways to skin a cat!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top