Using time in Select Case statement

G

Guest

Hello all,

I'm trying to use a time in a select case statment, along with the <>. It
doesn't work whith the <>. Is there a way to make this work?

vTime = Format(Time, "h:m")
vDate = Date

Select Case vTime

Case Is > "06:00"

If vTime < "17:59" Then

vSDate = vDate & " " & "6:00:00 AM"
vEDate = vDate & " " & "17:59:59 PM"

Else

vSDate = vDate & " " & "18:00:00 AM"
vEDate = DateAdd("d", 1, vDate) & " " & "05:59:59 PM"

End If

Case Is < "05:99"

vSDate = DateAdd("d", -1, vDate) & " " & "18:00:00 AM"
vEDate = vDate & " " & "05:59:59 PM"

End Select
 
M

Marshall Barton

Mark said:
I'm trying to use a time in a select case statment, along with the <>. It
doesn't work whith the <>. Is there a way to make this work?

vTime = Format(Time, "h:m")
vDate = Date

Select Case vTime

Case Is > "06:00"

If vTime < "17:59" Then

vSDate = vDate & " " & "6:00:00 AM"
vEDate = vDate & " " & "17:59:59 PM"

Else

vSDate = vDate & " " & "18:00:00 AM"
vEDate = DateAdd("d", 1, vDate) & " " & "05:59:59 PM"

End If

Case Is < "05:99"

vSDate = DateAdd("d", -1, vDate) & " " & "18:00:00 AM"
vEDate = vDate & " " & "05:59:59 PM"

End Select


The code for minutes is "n".
"m" is the code for month

You need to be sure that you get the leading zeros too.

vTime = Format(Time, "hh:nn")

you will also want to fix
Case Is < "05:99"
to
Case Is < "06:00"

If you insist on using "05:59", then you should use <=
 
T

Tim Ferguson

I'm trying to use a time in a select case statment, along with the <>.
It doesn't work whith the <>. Is there a way to make this work?

I don't really see the point of all this text-casting; presumably the
"v" prefix means these are all Variants anyway?

Still, you can do all this using proper DateTime variables:

dtTime = TimeValue(now())

If dtTime <= #06:00# Then
' it's early morning

ElseIf dtTime dtTime < #18:00# Then]
' it's daytime

Else ' If dtTime #18:00# Then
' it's in the evening

End If

I have to say that I find serial ElseIfs more intuitive for real numbers,
and I think that SELECT CASE should be reserved for discrete entities
like strings, integers, enums, and the like. Still, the same thing should
work:

Select Case TimeValue(Now())
Case Is < #06:00#
' it'e early
vSDate = DateAdd("h", -6, dtToday)
vEDate = DateAdd("s", 215999, dtToday)

Case Is < #18:00#
' it's daytime

Case Else
' it's evening

End Select


Hope that helps


Tim F
 

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