[VBA Excel] Text based on time
Solved
altra
-
altra -
altra -
Hello,
I'm trying to set up a macro that will display either "morning", "evening", or "night" in a cell depending on the time of day.
For example, between 5 AM and 1 PM, my cell should display "morning"; between 1 PM and 9 PM, "evening"; and between 9 PM and 5 AM, "night".
I would like all the code to be in a macro.
I've found some clues like
Range("mycell").Value=".......", but I'm not sure how to set my condition here.
Additionally, I don't know how to write this condition. I was thinking of something like this:
In fact, I don't know what variable I should declare or what type of variable it should be. I'm a bit lost.
Thank you for your help
I'm trying to set up a macro that will display either "morning", "evening", or "night" in a cell depending on the time of day.
For example, between 5 AM and 1 PM, my cell should display "morning"; between 1 PM and 9 PM, "evening"; and between 9 PM and 5 AM, "night".
I would like all the code to be in a macro.
I've found some clues like
Range("mycell").Value=".......", but I'm not sure how to set my condition here.
Additionally, I don't know how to write this condition. I was thinking of something like this:
if hour > 5 and hour < 13 then value="morning" elseif hour > 13 and hour < 21 then value="evening" else value="night" endif
In fact, I don't know what variable I should declare or what type of variable it should be. I'm a bit lost.
Thank you for your help
Configuration: Windows XP Internet Explorer 7.0
1 answer
I found a solution that meets my expectations, I'm posting it here in case someone needs it:
Have a good day
Sub hour() If Time >= "05:00:00" And Time < "13:00:00" Then Range("G1") = "Morning" ElseIf Time >= "13:00:00" And Time < "21:00:00" Then Range("G1") = "Evening" Else Range("G1") = "Night" End If End Sub Have a good day