Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Time Difference in VBA Macro

I'm trying to write a VBA macro to take the difference between two times
input by the user, calculate the difference, and put that value in the cell.

The first input box prompts for the start time (i.e. 9:00AM) and the second
prompts for the end time (i.e. 5:30PM). I've searched through this newsgroup
and done Google searches, but something just isn't sinking in. I need the
value to come out as a decimal value. With the 9:00 - 5:30 example, the cell
should show 8.5, but I keep getting -0.15. No doubt I'm missing something
obvious!

Thanks,
John
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Time Difference in VBA Macro

Post up the code so we can see what you are doing.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"John Schneider" wrote in message
...
I'm trying to write a VBA macro to take the difference between two times
input by the user, calculate the difference, and put that value in the
cell.

The first input box prompts for the start time (i.e. 9:00AM) and the
second
prompts for the end time (i.e. 5:30PM). I've searched through this
newsgroup
and done Google searches, but something just isn't sinking in. I need the
value to come out as a decimal value. With the 9:00 - 5:30 example, the
cell
should show 8.5, but I keep getting -0.15. No doubt I'm missing something
obvious!

Thanks,
John



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Time Difference in VBA Macro

Its definately a work in progress, but here's what I have so far:

Sub testjs()

Dim startTime As String
Dim endTime As String
Dim totHours As String

startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , ,
, , , 1)
endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , ,
, 1)

totHours = CDate(endTime) - CDate(startTime)

ActiveCell.Value = totHours

End Sub



"Bob Phillips" wrote:

Post up the code so we can see what you are doing.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"John Schneider" wrote in message
...
I'm trying to write a VBA macro to take the difference between two times
input by the user, calculate the difference, and put that value in the
cell.

The first input box prompts for the start time (i.e. 9:00AM) and the
second
prompts for the end time (i.e. 5:30PM). I've searched through this
newsgroup
and done Google searches, but something just isn't sinking in. I need the
value to come out as a decimal value. With the 9:00 - 5:30 example, the
cell
should show 8.5, but I keep getting -0.15. No doubt I'm missing something
obvious!

Thanks,
John




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 527
Default Time Difference in VBA Macro

Sub testjs()

Dim startTime As Date
Dim endTime As Date
Dim totHours As Date

startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , ,
, , , 1) * 24
endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , ,
, 1) * 24

totHours = endTime - startTime
ActiveCell.Value = totHours
ActiveCell.NumberFormat = "00.00"
End Sub

StartTime entered as 9:0
EndTime entered as 17:30

Is this OK?


"John Schneider" wrote:

Its definately a work in progress, but here's what I have so far:

Sub testjs()

Dim startTime As String
Dim endTime As String
Dim totHours As String

startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , ,
, , , 1)
endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , ,
, 1)

totHours = CDate(endTime) - CDate(startTime)

ActiveCell.Value = totHours

End Sub



"Bob Phillips" wrote:

Post up the code so we can see what you are doing.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"John Schneider" wrote in message
...
I'm trying to write a VBA macro to take the difference between two times
input by the user, calculate the difference, and put that value in the
cell.

The first input box prompts for the start time (i.e. 9:00AM) and the
second
prompts for the end time (i.e. 5:30PM). I've searched through this
newsgroup
and done Google searches, but something just isn't sinking in. I need the
value to come out as a decimal value. With the 9:00 - 5:30 example, the
cell
should show 8.5, but I keep getting -0.15. No doubt I'm missing something
obvious!

Thanks,
John




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Time Difference in VBA Macro

Billy,

That works perfect, but I don't understand why the time input (i.e. 9:00)
has to be multiplied by 24.

Thanks again!!!

"Billy Liddel" wrote:

Sub testjs()

Dim startTime As Date
Dim endTime As Date
Dim totHours As Date

startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , ,
, , , 1) * 24
endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , ,
, 1) * 24

totHours = endTime - startTime
ActiveCell.Value = totHours
ActiveCell.NumberFormat = "00.00"
End Sub

StartTime entered as 9:0
EndTime entered as 17:30

Is this OK?


"John Schneider" wrote:

Its definately a work in progress, but here's what I have so far:

Sub testjs()

Dim startTime As String
Dim endTime As String
Dim totHours As String

startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , ,
, , , 1)
endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , ,
, 1)

totHours = CDate(endTime) - CDate(startTime)

ActiveCell.Value = totHours

End Sub



"Bob Phillips" wrote:

Post up the code so we can see what you are doing.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"John Schneider" wrote in message
...
I'm trying to write a VBA macro to take the difference between two times
input by the user, calculate the difference, and put that value in the
cell.

The first input box prompts for the start time (i.e. 9:00AM) and the
second
prompts for the end time (i.e. 5:30PM). I've searched through this
newsgroup
and done Google searches, but something just isn't sinking in. I need the
value to come out as a decimal value. With the 9:00 - 5:30 example, the
cell
should show 8.5, but I keep getting -0.15. No doubt I'm missing something
obvious!

Thanks,
John





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 527
Default Time Difference in VBA Macro

John

Excel calculates dates as numbers, normally counting from 1/1/1900. Time is
calculated as fractions of a day. so 9:00 is 9/24 of a day. Bear this in mind
when making any calculations with time.

Peter

"John Schneider" wrote:

Billy,

That works perfect, but I don't understand why the time input (i.e. 9:00)
has to be multiplied by 24.

Thanks again!!!

"Billy Liddel" wrote:

Sub testjs()

Dim startTime As Date
Dim endTime As Date
Dim totHours As Date

startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , ,
, , , 1) * 24
endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , ,
, 1) * 24

totHours = endTime - startTime
ActiveCell.Value = totHours
ActiveCell.NumberFormat = "00.00"
End Sub

StartTime entered as 9:0
EndTime entered as 17:30

Is this OK?


"John Schneider" wrote:

Its definately a work in progress, but here's what I have so far:

Sub testjs()

Dim startTime As String
Dim endTime As String
Dim totHours As String

startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , ,
, , , 1)
endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , ,
, 1)

totHours = CDate(endTime) - CDate(startTime)

ActiveCell.Value = totHours

End Sub



"Bob Phillips" wrote:

Post up the code so we can see what you are doing.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"John Schneider" wrote in message
...
I'm trying to write a VBA macro to take the difference between two times
input by the user, calculate the difference, and put that value in the
cell.

The first input box prompts for the start time (i.e. 9:00AM) and the
second
prompts for the end time (i.e. 5:30PM). I've searched through this
newsgroup
and done Google searches, but something just isn't sinking in. I need the
value to come out as a decimal value. With the 9:00 - 5:30 example, the
cell
should show 8.5, but I keep getting -0.15. No doubt I'm missing something
obvious!

Thanks,
John



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
subtract the time difference from another time difference Dannigirl Excel Discussion (Misc queries) 3 September 30th 07 03:47 PM
Comparing 2 files on date/time stamp, and based time difference do a subroutine [email protected] Excel Programming 1 September 28th 07 03:53 AM
Daylight Saving Time (DST) and calculating time difference. Chip Pearson Excel Programming 1 January 17th 07 03:35 PM
Time difference calculations, daylight savings time, Excel Tim Excel Discussion (Misc queries) 1 December 28th 06 04:18 PM
Help with difference in time macro chin_un_len Excel Programming 6 March 2nd 06 02:25 PM


All times are GMT +1. The time now is 10:14 AM.

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"