excel VBA code

S

Savio

Any idea how to use VBA code to check if the current day is a
particular day ?
example : check if today is saturday
thanks
 
M

muddan madhu

Sub test()
dys = Format(Date, "dddd")
MsgBox "Today is " & UCase(dys)
End Sub
 
S

Savio

thanks. do you know how i could incorporate that into an if statement.
Say : if today is saturday, follow a set of commands, else do
something else?
 
M

muddan madhu

Sub test()
dys = Format(Date, "dddd")
If dys = "saturday" Then
' your code goes here
Else
' your code
End If
End Sub
 
D

Dana DeLouis

Instead of generating a string ("saturday") perhaps...

If Day(Date) = vbSaturday Then
'Your code...
Else
'Something else
End If

= = =
Dana DeLouis
 
S

Shane Devenshire

Hi,

The Day function does not return the weekday it returns the day of the month.

Instead you could use

If Weekday(Date) = 7 Then

Else

End if

7 is the value for Saturday; Sunday = 1. If you're not coding a msgbox then
this would be a possibe branch.

If you want to handle the entire week consider using

Select Case Weekday(Date)
Case 1 'Sunday
'your code here
Case 2 'Monday
...

Case Else

End Select
 
D

Dana DeLouis

Instead you could use
If Weekday(Date) = 7 Then

Oops! Thanks for the catch. Yes, I meant Weekday.

Instead of a string, or a number like 7, perhaps...

? VbSaturday
7

Hence...

If Weekday(Date) = vbSaturday Then...

= = =
Thanks again.
Dana
 
C

Chris Bode via OfficeKB.com

You can use the following macro code to do that.
Please follow following steps
1.Right click on toolbar and click control box
2.From the control box that aooears, select command button and draw it on
your sheet
3.Double click on the command button to open the code and paste following
codes
Private Sub CommandButton1_Click()
Dim dt As Date

If Weekday(Now) = 7 Then
MsgBox "today is Saturday"
End If
End Sub

Hope you get it!

Have a nice time

Chris
 
Top