Time difference using vba

roipatrick

New Member
Joined
Mar 20, 2014
Messages
39
I need help on how to subtract two military time in vba.

Here's how i grab the start time
Code:
Sub Start_Timer_Button()    'Call Clear
    Worksheets("Control Panel").Cells.Locked = False
    Range("L16").Value = Time
    Worksheets("Control Panel").Range("L16").Locked = True
    Worksheets("Control Panel").Protect Password:="Str0ngp@ssw0rd", UserInterfaceOnly:=True
End Sub

Here's how I end the time
Code:
Sub Stop_Timer_Button()    Worksheets("Control Panel").Cells.Locked = False
    Range("L18").Value = Time
    Worksheets("Control Panel").Range("L16").Locked = True
    Worksheets("Control Panel").Range("L18").Locked = True
    Worksheets("Control Panel").Protect Password:="Str0ngp@ssw0rd", UserInterfaceOnly:=True
    Call Compute
End Sub

As you can see I call a compute procedure, Here's the procedure

Code:
Sub Compute()    Dim x As Date
    Dim y As Date
    Dim t As Date
    
    x = Worksheets("Control Panel").Range("L18").Value
    y = Worksheets("Control Panel").Range("L16").Value
    MsgBox CDbl(TimeValue(x)) * 24
    MsgBox CDbl(TimeValue(y)) * 24
    zoo = x - y
    
    t = zoo / 24
    MsgBox "d = " & d & vbLf & "t = " & t
    
    
End Sub


It keep returning me this result "12:00:00 AM"

Where did i do wrong ?

Thanks in advance for the help
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
When you assign Time to L16 and L18 you haven't qualified the Range with its Worksheet object (Control Panel). Maybe the data is on a different sheet.
 
Upvote 0
Yes but you have:

Range("L16").Value = Time

rather than:

Worksheets("Control Panel").Range("L16").Value = Time
 
Upvote 0

Forum statistics

Threads
1,215,151
Messages
6,123,316
Members
449,094
Latest member
Chestertim

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