Code Not Converting Text Time To Real Time

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am using this line of code to place a text time value into a cell, convert it to a valid time number, and format it as "h:mmA/P"

Code:
ws_cd.Cells(r, 27) = Format(CDate(.Cells(rp, 11)), "h:mmA/P") 'light off

The format is right, but I don't believe the time is true. (ie if I try to manually change the format to a number I still get the text).4

What needs tweaking?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Show us a typical value that is in .Cells(rp, 11) and show us what gets placed in ws_cd.Cells(r, 27) so that we have some idea what you are talking about.
 
Upvote 0
Hi Rick, here is the data for example at .cells(5,11) from the source data. I've also included the results of the isnumber function for that cell confirming(?) it isn't a number. PEBF2.xlsx is the source worksheet. (different workbook)
Rich (BB code):
ws_cd.Cells(r, 27) = Format(CDate(.Cells(rp, 11)), "h:mmA/P") 'light off


PEBF2.xlsx
KL
59:00 PMFALSE
Sheet1
Cell Formulas
RangeFormula
L5L5=ISNUMBER(K5)


And this is that value placed at ws_cd.cells(3,27) for example, with the results (for debugging purposes) of isnumber also shown. 'task allocation.xlsx' is the destination worksheet (different workbook)
Rich (BB code):
ws_cd.Cells(r, 27) = Format(CDate(.Cells(rp, 11)), "h:mmA/P") 'light off


task allocation.xlsm
AAABAC
39:00PFALSE
Core_Data
Cell Formulas
RangeFormula
AC3AC3=ISNUMBER(AA2)


I hope this helps.
 
Upvote 0
It would appear that Excel doesn't process my time format.
Although I have used this format throughout my application, in this case it isn't working. I'll have to go back and check the other instances where this format is used to ensure that the results are indeed time, and not text. But I have not received any errors or unusual behaviour with the values having this format.
 
Upvote 0
The value placed in a cell using this line of code...
VBA Code:
ws_cd.Cells(r, 27) = Format(CDate(.Cells(rp, 11)), "h:mmA/P") 'light off

is Text, not a date... since that text is not in a form Excel recognizes as a Date, Excel simply leaves it as Text. What I think you would need to do is output the real date to Excel and then let Excel format it the way you want. Something like this...
VBA Code:
ws_cd.Cells(r, 27) = CDate(.Cells(rp, 11)) 'light off
ws_cd.Cells(r, 27).NumberFormat = "h:mmA/P"
 
Last edited:
Upvote 0
Hi Rick, thank you for sharing that last post.

I improved my code with your suggestion, but just curious ... why would this work for me?
Code:
ws_cd.Cells(r, 27) = Format(CDate(.Cells(rp, 11)), "h:mm am/pm") 'light off
 
Upvote 0
Because you have a space between the time and the AM or PM that the Format function outputs... that is a recognizable time value so Excel converts it into a real time... without the space, the text is not in a recognizable time format to Excel so it leaves it alone.
 
Upvote 0
Interesting. The workaround then is to use 'NumberFormat'. Great lesson!
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,614
Members
449,039
Latest member
Mbone Mathonsi

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