Using time with Select Case

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
What am I doing wrong? The time now is 3:24 AM and when I run this code, the greeting should be equal to "Good morning" but it's not. Instead, it's equal to "Good evening". Thank you.
VBA Code:
Sub greetings()
    Select Case Time()
        Case "12:00:00 AM" To "12:00:00 PM"
            Greeting = "Good morning, "
        Case "12:00:01 PM" To "18:00:00 PM"
            Greeting = "Good afternoon, "
        Case Else
            Greeting = "Good evening, "
    End Select
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You should use litteral date values not strings
VBA Code:
    Select Case time()
        Case #12:00:00 AM# To #12:00:00 PM#
            Greeting = "Good morning, "
        Case #12:00:01 PM# To #18:00:00 PM#
            Greeting = "Good afternoon, "
        Case Else
            Greeting = "Good evening, "
    End Select

PS: Or convert the strings to dates with the CDate function.
 
Upvote 0
Solution
I'm still kinda new to VBA. Can you explain the difference between the two. Thanks
 
Upvote 0
I'm still kinda new to VBA. Can you explain the difference between the two. Thanks
The Time vba function returns a variable of Date type
The reason your code didn't work is because you were comparing String values against Date values (returned by the Time function)
To make it work, use Date literals which must be enclosed within number signs (#) or, you could use the CDate vba function to convert the String values to Dates/times as follows:
VBA Code:
 Select Case time()
        Case CDate("12:00:00 AM") To CDate("12:00:00 PM")
            Greeting = "Good morning, "
        Case CDate("12:00:01 PM") To CDate("18:00:00 PM")
            Greeting = "Good afternoon, "
        Case Else
            Greeting = "Good evening, "
 End Select
 
Upvote 0
Ok. That makes sense. I've seen the "CDate" used. I don't think I've seen the # used before. Thank you for the explanation.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top