Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Userform formats time incorrectly

Would someone help me understand this, please? In my user form, only
particular numbers format incorrectly into time with the following
code. (This is much simplified from the original, but still produces
the same problem.)

Private Sub UserForm_Initialize()

'This actually pulls in a variable from a cell, but shown with a
constant here.
TextBox1.Value = 0.25

'Later in the code, this formats the textbox to display time.
TextBox1.Value = Format(TextBox1.Value, "h:mm")

End Sub

It works with every number but .25 (6am) and .5 (noon). They format
incorrectly as "0:25" (12:25 am) and "0.05" (12:05 am), respectively.
All other numbers work, but I need it to work for these times as well.

???

Thanks,
Chuck

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Userform formats time incorrectly

Chuck,
Coerce the value to a single before the formatting:

TextBox1.Value = Format(CSng(TextBox1.Value), "h:mm")

NickHK

"c mateland" wrote in message
oups.com...
Would someone help me understand this, please? In my user form, only
particular numbers format incorrectly into time with the following
code. (This is much simplified from the original, but still produces
the same problem.)

Private Sub UserForm_Initialize()

'This actually pulls in a variable from a cell, but shown with a
constant here.
TextBox1.Value = 0.25

'Later in the code, this formats the textbox to display time.
TextBox1.Value = Format(TextBox1.Value, "h:mm")

End Sub

It works with every number but .25 (6am) and .5 (noon). They format
incorrectly as "0:25" (12:25 am) and "0.05" (12:05 am), respectively.
All other numbers work, but I need it to work for these times as well.

???

Thanks,
Chuck



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Userform formats time incorrectly

Thanks, Nick. I'll give it a try.

I still don't understand some things, however. If not already a single
data type, what was it? Why did it function correctly for all serial
numbers except .25 and .5? Why does it function correctly if I
reference the cell value directly in the format command:

TextBox1.Value = Format(range("a1").value, "h:mm")

Just trying to understand so I know WHEN to do this and when I don't
need to.

Thanks,
Chuck


NickHK wrote:
Chuck,
Coerce the value to a single before the formatting:

TextBox1.Value = Format(CSng(TextBox1.Value), "h:mm")

NickHK

"c mateland" wrote in message
oups.com...
Would someone help me understand this, please? In my user form, only
particular numbers format incorrectly into time with the following
code. (This is much simplified from the original, but still produces
the same problem.)

Private Sub UserForm_Initialize()

'This actually pulls in a variable from a cell, but shown with a
constant here.
TextBox1.Value = 0.25

'Later in the code, this formats the textbox to display time.
TextBox1.Value = Format(TextBox1.Value, "h:mm")

End Sub

It works with every number but .25 (6am) and .5 (noon). They format
incorrectly as "0:25" (12:25 am) and "0.05" (12:05 am), respectively.
All other numbers work, but I need it to work for these times as well.

???

Thanks,
Chuck


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Userform formats time incorrectly

I can't repeat you results. From the immediate window, I get:

?format("0.1", "h:mm")
0:01
?format(0.1, "h:mm")
2:24
?format("0.5", "h:mm")
0:05
?format(0.5, "h:mm")
12:00
?format("1", "h:mm")
0:00
?format(1, "h:mm")
0:00

And the Range.Value is a Variant, so it's sub-type could numeric, in which
case Format to time would be correct.
However, TextBox.Value is always a String, so you have to rely on VBA
guessing what should happen correctly, which (apparently) it does not.
The CSng() ensures the correct data type is used for the formatting.

NickHK

"c mateland" wrote in message
ups.com...
Thanks, Nick. I'll give it a try.

I still don't understand some things, however. If not already a single
data type, what was it? Why did it function correctly for all serial
numbers except .25 and .5? Why does it function correctly if I
reference the cell value directly in the format command:

TextBox1.Value = Format(range("a1").value, "h:mm")

Just trying to understand so I know WHEN to do this and when I don't
need to.

Thanks,
Chuck


NickHK wrote:
Chuck,
Coerce the value to a single before the formatting:

TextBox1.Value = Format(CSng(TextBox1.Value), "h:mm")

NickHK

"c mateland" wrote in message
oups.com...
Would someone help me understand this, please? In my user form, only
particular numbers format incorrectly into time with the following
code. (This is much simplified from the original, but still produces
the same problem.)

Private Sub UserForm_Initialize()

'This actually pulls in a variable from a cell, but shown with a
constant here.
TextBox1.Value = 0.25

'Later in the code, this formats the textbox to display time.
TextBox1.Value = Format(TextBox1.Value, "h:mm")

End Sub

It works with every number but .25 (6am) and .5 (noon). They format
incorrectly as "0:25" (12:25 am) and "0.05" (12:05 am), respectively.
All other numbers work, but I need it to work for these times as well.

???

Thanks,
Chuck




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
Bug ?? - Excel 2007 Format Wizard formats values incorrectly (icon George Excel Discussion (Misc queries) 1 February 3rd 10 11:28 AM
Chart incorrectly displays time of day on x-axis SPH1967 Excel Discussion (Misc queries) 3 February 15th 09 06:16 PM
Difference between time in excel reported incorrectly Jon Excel Discussion (Misc queries) 6 December 19th 08 04:23 PM
How to get Excel to stop formatting time cells incorrectly Chuck Cusack Excel Discussion (Misc queries) 2 August 6th 05 01:10 AM
Pivot Tables : Total Time incorrectly computed François Excel Programming 1 April 12th 05 04:03 PM


All times are GMT +1. The time now is 09:18 AM.

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"