PC Review


Reply
Thread Tools Rate Thread

Adding time by a Numerical Value

 
 
Corey
Guest
Posts: n/a
 
      14th Aug 2008
I have a cell with a format of "hh:mm AM/PM" and
another cell in a numerical format of "0.75".

I want to using VBA, add the numerical value to the time to create a new
time, such as:

A1= 7:30 AM
B1 = 0.75 ' 45mins
C1= 8:15 AM ' Result.

I cannot seem to get the correct syntax to add the value to give me the
desired result.

If i format the numerical value as "hh:mm" it changes from 0.75 to 18:00 ?

How can i do this:

Corey....


 
Reply With Quote
 
 
 
 
IanKR
Guest
Posts: n/a
 
      15th Aug 2008
>I have a cell with a format of "hh:mm AM/PM" and
> another cell in a numerical format of "0.75".
>
> I want to using VBA, add the numerical value to the time to create a new
> time, such as:
>
> A1= 7:30 AM
> B1 = 0.75 ' 45mins
> C1= 8:15 AM ' Result.
>
> I cannot seem to get the correct syntax to add the value to give me the
> desired result.
>
> If i format the numerical value as "hh:mm" it changes from 0.75 to 18:00 ?
>
> How can i do this:
>
> Corey....



This worked for me:

Sub AddTimeAsNumber()
Dim num As Integer
num = Hour(Range("A1")) * 60 _
+ Minute(Range("A1")) _
+ Range("B1") * 60
Range("C1") = Format(WorksheetFunction.RoundDown(num / 60, 0) _
& ":" & num Mod 60, "h:mm\ AM/PM")
End Sub

.... but there's probably a more elegant way!

Ian

 
Reply With Quote
 
IanKR
Guest
Posts: n/a
 
      15th Aug 2008
> If i format the numerical value as "hh:mm" it changes from 0.75 to 18:00 ?

That's because 18:00 (6pm) is three-quarters of the way through the day.
 
Reply With Quote
 
GTVT06
Guest
Posts: n/a
 
      15th Aug 2008
Hello, you can try this:

Sub timeval()

Range("C1").Value = Range("A1") + TimeSerial(0, Minute("12:" &
Range("B1") * 60), 0)

End Sub


 
Reply With Quote
 
Corey
Guest
Posts: n/a
 
      15th Aug 2008
Thanks Ian.

Perfect.
"IanKR" <ik.rip_@_ntlworld.com> wrote in message
news:uu4fhZm$(E-Mail Removed)...
> >I have a cell with a format of "hh:mm AM/PM" and
>> another cell in a numerical format of "0.75".
>>
>> I want to using VBA, add the numerical value to the time to create a new
>> time, such as:
>>
>> A1= 7:30 AM
>> B1 = 0.75 ' 45mins
>> C1= 8:15 AM ' Result.
>>
>> I cannot seem to get the correct syntax to add the value to give me the
>> desired result.
>>
>> If i format the numerical value as "hh:mm" it changes from 0.75 to 18:00
>> ?
>>
>> How can i do this:
>>
>> Corey....

>
>
> This worked for me:
>
> Sub AddTimeAsNumber()
> Dim num As Integer
> num = Hour(Range("A1")) * 60 _
> + Minute(Range("A1")) _
> + Range("B1") * 60
> Range("C1") = Format(WorksheetFunction.RoundDown(num / 60, 0) _
> & ":" & num Mod 60, "h:mm\ AM/PM")
> End Sub
>
> ... but there's probably a more elegant way!
>
> Ian



 
Reply With Quote
 
IanKR
Guest
Posts: n/a
 
      15th Aug 2008
> Hello, you can try this:
>
> Sub timeval()
>
> Range("C1").Value = Range("A1") + TimeSerial(0, Minute("12:" &
> Range("B1") * 60), 0)
>
> End Sub


!!

I knew somebody would come up with a slicker formula then mine. I didn't
know about TimeSerial - equivalent to DateSerial, of course.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a Leter to a Numerical Value on Exit Corey .... Microsoft Excel Programming 5 7th Jul 2008 06:28 AM
Giving letters a numerical value, then adding the total. =?Utf-8?B?UmljaGFyZCBpbiBTdG9ja2hvbG0=?= Microsoft Excel Misc 17 9th Apr 2008 05:01 PM
Adding up non numerical values =?Utf-8?B?V29vc3Rlcg==?= Microsoft Excel New Users 3 23rd Sep 2007 01:03 AM
Numerical fields mysteriously adding together =?Utf-8?B?U3Vl?= Microsoft Access 4 12th Jan 2007 03:13 AM
Re: Adding numbers to an incomplete numerical list T. Valko Microsoft Excel Misc 6 11th Jan 2007 09:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:55 AM.