• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Codes to seggregate hours into 12

Hi friends,

I have datetime field in column M

I will take only Hour field from there.

After that if an hour is between

0-2 then "0AM-2AM"

3-4 means "2AM-4AM"

5-6 Means "4AM - 6 AM"


.....Like that it follows

the last one will be "10PM-12 AM".

Kindly help me to get VBA Code.

Regds,

Hanu
 
If you really need a VB solution:
Code:
Function TimeCode(myDate As Date) As String
Dim myHour As Long
Dim roundTime As Date

roundTime = WorksheetFunction.Floor(myDate, 2 / 24)

TimeCode = Format(roundTime, "hAM/PM") & " - " & Format(roundTime + 2 / 24, "hAM/PM")
End Function

Or, even faster, the regular formula:
=TEXT(FLOOR(M2,2/24),"hAM/PM")&" - "&TEXT(FLOOR(M2,2/24)+2/24,"hAM/PM")
 
Back
Top