Subtract Textbox Time Values

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,069
I would like a vba script which deducts 2 textbox time values as follows.

textbox1.value = 11:40:00
textbox2.value = 11:49:59

I would like the final value to be displayed as 9:59
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I have found a script which sort of works, however it won't update correctly, ie. it goes from 790 to 789 to 787 to 786 to 784,

For some strange reason it keeps skipping values and therefore is really doubling the actual time difference.

Code:
Dim x As Variant
    Dim y As Variant
    Dim z As Single
     
    x = CDate(Clock.TextBox12)  '12 is now time
    y = CDate(Clock.TextBox11)  '11 is form time + 9.30
    z = Round((y - x) * 12, 4)
    'If z < 0 Then z = z + 12
    Clock.TextBox14 = z
Clock.TextBox14.Value = Clock.TextBox14.Value * 10000
 
Upvote 0
Not sure if this may be helpful. But an other approach might be to put your times in spreadsheet cells. Such as :

Cell A1 has 11:40:00
Cell A2 has 11:49:59
Cell A3 has formula "=A2-A1"

Then if A1 is increasing in time on the screen, then cell A3 will be decreasing in time towards 0. Then you could process cell A3 to put its data into your desired text box to show the time counting down.

Hope that is of use.

Chuck
 
Upvote 0
Sorry, I didn't see your reply Chuck,

I can't use cells as the textboxes update via a wintimer which runs independently and cells would cause massive problems.
 
Upvote 0
This worked for me.
Code:
Private Sub UserForm_Click()
    With Label1
        .Caption = Format(TimeValue(TextBox2.Text) - TimeValue(TextBox1.Text), "h:m:ss")
        .Caption = Mid(.Caption, InStr(1, .Caption, ":") + 1)
    End With
End Sub
The format "m:ss" returned month:second,second :confused:
 
Upvote 0
I can't get it to work Mike it keeps crashing excel.

I have tried changing it to relate to my textboxes, but no go.

Can you please relate it to the following script

Clock.textbox4.value =clock.textbox12.value - clock.textbox11.value

And then it would update each second within my timer.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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