How can I verify that a cell contains a valid time?

USAMax

Well-known Member
Joined
May 31, 2006
Messages
843
Office Version
  1. 365
Platform
  1. Windows
I currently have a cell that reads 8:00 AM or .3333 depending on how it is formatted. The cell will always be formatted "h:mm AM/PM" but could be blank, have a space " " or have a time including 12:00 AM which is 0. Sometimes IsDate works but not always.

How can I verify that a cell contains a valid time?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I currently have a cell that reads 8:00 AM or .3333 depending on how it is formatted. The cell will always be formatted "h:mm AM/PM" but could be blank, have a space " " or have a time including 12:00 AM which is 0. Sometimes IsDate works but not always.

How can I verify that a cell contains a valid time?
I think first you have to tell us why you think 12:00 AM is not a valid time (otherwise we will not know how to construct the VB code).
 
Upvote 0
1] A1 =8:00A.M.

2] A2 =0.3333

3] Cell B1 enter the formula and copy down to B2 :

=IF(LEFT(CELL("format",A1))="D","Valid Time","Value")

Regards
 
Upvote 0
I was not very clear when explaining my problem. 12:00 AM is a valid time but as its value is Zero my testing has not worked for this time.

My biggest error was not explaining that I need to know how to detect this in VBA and not on the worksheet. I will be working with different durations such as 1:15 or one hour and 15 minutes and adding this to the time of day. For example: 8:00 AM plus 1:15 is 9:15 AM. I need to validate the times in both formats.

My thanks to both of you for your input.
 
Upvote 0
I was not very clear when explaining my problem. 12:00 AM is a valid time but as its value is Zero my testing has not worked for this time.

My biggest error was not explaining that I need to know how to detect this in VBA and not on the worksheet. I will be working with different durations such as 1:15 or one hour and 15 minutes and adding this to the time of day. For example: 8:00 AM plus 1:15 is 9:15 AM. I need to validate the times in both formats.
Assuming the cell you want to check will never contain a number for a time value over 24 hours (since that would show as a time but be for the next day), then I think this should work...

Code:
Function IsRealTime(Cell As Range) As Boolean
  IsRealTime = (Len(Trim(Cell.Value)) > 0 And Cell.Value >= 0 And Cell.Value < 1)
End Function
 
Upvote 0
This looks sound but I am not sure it will work when the cell has a " " but I am happy to try it.

I will be testing data for the next two hours and if it works I will let you know tomorrow.

Thank you Rick!
 
Upvote 0
This looks sound but I am not sure it will work when the cell has a " " but I am happy to try it.

I will be testing data for the next two hours and if it works I will let you know tomorrow.
It will work for a space (or any arbitrary text for that matter) entered into the cell. Yes, definitely let us know how it worked for you and, if it doesn't work, please describe under what situation so we can patch the code for you.
 
Upvote 0
Assuming the cell you want to check will never contain a number for a time value over 24 hours (since that would show as a time but be for the next day), then I think this should work...

Code:
Function IsRealTime(Cell As Range) As Boolean
  IsRealTime = (Len(Trim(Cell.Value)) > 0 And Cell.Value >= 0 And Cell.Value < 1)
End Function

Good morning Rick,

Thank you again for the perfect solution. Your code was so elegant that it has inspired me to change several of my existing functions to work the same way.

In reality, you gave me three solutions while helping me with one.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,865
Messages
6,121,988
Members
449,060
Latest member
mtsheetz

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