Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default If then statement based on time of day

Hello-

I am trying to update a cell with text (time of day) based on the time
the report is actually run. The report runs at roughly 15 past the
hour 3 times a day, but I dont want the report to show the actual time
but rather the "top of the hour" time as shown in the code. The code
doesnt work though, any ideas?

Range("A2").Select
If Time < TimeSerial(11, 30, 0) Then
Range("A2").Select
ActiveCell.FormulaR1C1 = "11:00"
ElseIf Time < TimeSerial(2, 30, 0) Then
Range("A2").Select
ActiveCell.FormulaR1C1 = "2:00"
ElseIf Time < TimeSerial(5, 30, 0) Then
Range("A2").Select
ActiveCell.FormulaR1C1 = "5:00"
End If
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default If then statement based on time of day

On Jun 22, 4:46*pm, Sabosis wrote:
I am trying to update a cell with text (time of day) based
on the time the report is actually run. The report runs at
roughly 15 past the hour 3 times a day, but I dont want the
report to show the actual time but rather the "top of the
hour" time as shown in the code. The code doesnt work though,
any ideas?


In what way does it not work? Gives examples. "When the time is
this, I expect that, but I get this instead".

Range("A2").Select
If Time < TimeSerial(11, 30, 0) Then
Range("A2").Select
ActiveCell.FormulaR1C1 = "11:00"
ElseIf Time < TimeSerial(2, 30, 0) Then
Range("A2").Select
ActiveCell.FormulaR1C1 = "2:00"
ElseIf Time < TimeSerial(5, 30, 0) Then
Range("A2").Select
ActiveCell.FormulaR1C1 = "5:00"
End If


I suspect you want:

Sub doit()
Dim t As Date
Select Case Time
Case Is < TimeSerial(11, 30, 0)
t = TimeSerial(11, 0, 0) '11:00 AM
Case Is < TimeSerial(14, 30, 0)
t = TimeSerial(14, 0, 0) '2:00 PM
Case Else
t = TimeSerial(17, 0, 0) '5:00 PM
End Select
Range("A2") = t
Range("A2").NumberFormat = "h:mm am/pm"
End Sub

But I don't understand that logic. I presume that the report
__should_be__ at 11:00-, 2:00- and 5:30-ish. But why not capture the
hour of the day that the report is actually run?

To that end:

Sub doit()
Range("A2") = TimeSerial(Hour(Time), 0, 0)
Range("A2").NumberFormat = "h:mm am/pm"
End Sub

In both cases, add Range("A2").Select only if you want the cursor to
be positioned in A2 when you are done. Normally, that is not the case.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default If then statement based on time of day

PS....

Geesh, Google Groups is slow today.

On Jun 22, 7:16*pm, joeu2004 wrote:
Sub doit()
Dim t As Date
Select Case Time
* Case Is < TimeSerial(11, 30, 0)
* * *t = TimeSerial(11, 0, 0) * '11:00 AM
* Case Is < TimeSerial(14, 30, 0)
* * *t = TimeSerial(14, 0, 0) * '2:00 PM
* Case Else
* * *t = TimeSerial(17, 0, 0) * '5:00 PM
End Select
Range("A2") = t
Range("A2").NumberFormat = "h:mm am/pm"
End Sub


I a.s.s-u-me-d the interesting times were intended to be 11:00 AM,
2:00 PM and 5:00 PM. You never explained. Sigh.

If instead you truly meant 2:00 AM, 5:00 AM and 11:00 AM (!), you
needed to change the order of some of your comparisons. Namely:

Sub doit()
Dim t As Date
Select Case Time
Case Is < TimeSerial(2, 30, 0)
t = TimeSerial(2, 0, 0) '2:00 AM
Case Is < TimeSerial(5, 30, 0)
t = TimeSerial(5, 0, 0) '5:00 AM
Case Else
t = TimeSerial(11, 0, 0) '11:00 PM
End Select
Range("A2") = t
Range("A2").NumberFormat = "h:mm am/pm"
End Sub

Note that Select is order-dependent. It is equivalent to writing If-
Then-ElseIf-Else statements.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default If then statement based on time of day

On Jun 22, 7:31*pm, joeu2004 wrote:
PS....

Geesh, Google Groups is slow today.

On Jun 22, 7:16*pm, joeu2004 wrote:

Sub doit()
Dim t As Date
Select Case Time
* Case Is < TimeSerial(11, 30, 0)
* * *t = TimeSerial(11, 0, 0) * '11:00 AM
* Case Is < TimeSerial(14, 30, 0)
* * *t = TimeSerial(14, 0, 0) * '2:00 PM
* Case Else
* * *t = TimeSerial(17, 0, 0) * '5:00 PM
End Select
Range("A2") = t
Range("A2").NumberFormat = "h:mm am/pm"
End Sub


I a.s.s-u-me-d the interesting times were intended to be 11:00 AM,
2:00 PM and 5:00 PM. *You never explained. *Sigh.

If instead you truly meant 2:00 AM, 5:00 AM and 11:00 AM (!), you
needed to change the order of some of your comparisons. *Namely:

Sub doit()
Dim t As Date
Select Case Time
* *Case Is < TimeSerial(2, 30, 0)
* * * t = TimeSerial(2, 0, 0) * *'2:00 AM
* *Case Is < TimeSerial(5, 30, 0)
* * * t = TimeSerial(5, 0, 0) * *'5:00 AM
* *Case Else
* * * t = TimeSerial(11, 0, 0) * '11:00 PM
End Select
Range("A2") = t
Range("A2").NumberFormat = "h:mm am/pm"
End Sub

Note that Select is order-dependent. *It is equivalent to writing If-
Then-ElseIf-Else statements.


Thanks, this worked perfectly, I appreciate the help.

Scott
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
If statement to compare time cell to a time Z-Man-Cek Excel Worksheet Functions 16 July 29th 16 08:17 AM
Comparing 2 files on date/time stamp, and based time difference do a subroutine [email protected] Excel Programming 1 September 28th 07 03:53 AM
IF statement to calculate time usage in specific time bands Daren Excel Worksheet Functions 6 January 31st 07 01:34 PM
IF statement for time based data Daren Excel Worksheet Functions 13 October 22nd 06 04:19 AM
Using an IF statement on time based data DonB Excel Discussion (Misc queries) 11 December 3rd 05 04:07 AM


All times are GMT +1. The time now is 09:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"