Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Trouble formatting a date variable to contain a time value

I want my user to enter a time into an input box and store the value in
a date variable. Here's my code:

Dim MyTime As Date
MyTime = InputBox("Enter Time", "Time", Format(Time, "hhmm")) / 2400

I have the "/ 2400" on the end because that forces the value entered to
be a time; without that, the value my user enters becomes a date.

The trouble is that the code above returns a value for minutes that's
three fifths of the correct value. It gets the hour correct; for
example, if my user enters "1500", the value is stored correctly. But if
my user enters "1510", the value stored is 1506. In fact, whatever
number of minutes my user enters, the value stored is 3/5 of the correct
value; it sees hours as only 36 minutes long.

Can someone tell me where I've made my mistake? Thanks.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Trouble formatting a date variable to contain a time value

by dividing by 2400, you're effectively using hours and centihours
(hundredths of hours) rather than hours and minutes.

One alternative:

Dim MyTime As Date
MyTime = TimeValue(Format(Application.InputBox( _
Prompt:="Enter Time", _
Title:="Time", _
Default:=Format(Time, "hhmm"), _
Type:=1), _
"00\:00"))



In article ,
Arch Stanton wrote:

I want my user to enter a time into an input box and store the value in
a date variable. Here's my code:

Dim MyTime As Date
MyTime = InputBox("Enter Time", "Time", Format(Time, "hhmm")) / 2400

I have the "/ 2400" on the end because that forces the value entered to
be a time; without that, the value my user enters becomes a date.

The trouble is that the code above returns a value for minutes that's
three fifths of the correct value. It gets the hour correct; for
example, if my user enters "1500", the value is stored correctly. But if
my user enters "1510", the value stored is 1506. In fact, whatever
number of minutes my user enters, the value stored is 3/5 of the correct
value; it sees hours as only 36 minutes long.

Can someone tell me where I've made my mistake? Thanks.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Trouble formatting a date variable to contain a time value

Your solution works perfectly. Thanks, and thanks also for the hint
about dividing by 2400 (didn't really understand why I was doing that in
the first place).

If I could ask one more thing, what does the slash in the format string
("00\:00") do? I can't find it in VBA Help, but it seems to work.

AS


JE McGimpsey wrote:
by dividing by 2400, you're effectively using hours and centihours
(hundredths of hours) rather than hours and minutes.

One alternative:

Dim MyTime As Date
MyTime = TimeValue(Format(Application.InputBox( _
Prompt:="Enter Time", _
Title:="Time", _
Default:=Format(Time, "hhmm"), _
Type:=1), _
"00\:00"))



In article ,
Arch Stanton wrote:

I want my user to enter a time into an input box and store the value in
a date variable. Here's my code:

Dim MyTime As Date
MyTime = InputBox("Enter Time", "Time", Format(Time, "hhmm")) / 2400

I have the "/ 2400" on the end because that forces the value entered to
be a time; without that, the value my user enters becomes a date.

The trouble is that the code above returns a value for minutes that's
three fifths of the correct value. It gets the hour correct; for
example, if my user enters "1500", the value is stored correctly. But if
my user enters "1510", the value stored is 1506. In fact, whatever
number of minutes my user enters, the value stored is 3/5 of the correct
value; it sees hours as only 36 minutes long.

Can someone tell me where I've made my mistake? Thanks.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Trouble formatting a date variable to contain a time value

In the immediate window, type

?Format(1510,"00\:00")

then

?TimeValue(Format(1510,"00\:00"))

In article ,
Arch Stanton wrote:

If I could ask one more thing, what does the slash in the format string
("00\:00") do? I can't find it in VBA Help, but it seems to work.

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
Trouble formatting date geordy67 Excel Worksheet Functions 6 July 27th 06 02:30 AM
Trouble formatting Date data LB Charts and Charting in Excel 1 May 1st 06 12:11 PM
Trouble with OpenText() and Date/Time Values rmac Excel Programming 0 March 20th 06 03:33 PM
Put only date (not time too) in variable from FileDateTime function mikeburg[_64_] Excel Programming 3 January 22nd 06 06:10 AM
Trouble setting variable as filename with date Elby Excel Programming 2 February 21st 04 03:05 PM


All times are GMT +1. The time now is 12:38 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"