Adding Time Within Textboxes

Try2Live4God

New Member
Joined
May 17, 2006
Messages
14
Hi,
I was trying to search but couldn't find anything related to what I'd like to do. But then again, I'm still somewhat of a novice with VBA..... :-)

Anyways....I have 4 textboxes that have the total overtime hrs. I'd like to add up the values/times from these 4 textboxes and get a Grand Total of the OT hrs which will display in a fifth textbox. I want to stick to using textboxes and not use the cells.

Example:
Textbox1 = 1:00 of OT
Textbox2 = 1:30 of OT
Textbox3 = 2:00 of OT
Textbox4 = 2:45 of OT
Textbox5 = Grand Total of 7:15

Any help would be appreciated.

Thanks!
 
As per what you posted in Message #5 , assuming your five TextBoxes are named txtCash1, txtCash2, txtCash3, txtCash4 and txtGrandTotalCash1, replace the code you now have for your txtCash# TextBoxes with this...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub txtCash1_Change()
  Call SumTimes
End Sub

Private Sub txtCash2_Change()
  Call SumTimes
End Sub

Private Sub txtCash3_Change()
  Call SumTimes
End Sub

Private Sub txtCash4_Change()
  Call SumTimes
End Sub[/td]
[/tr]
[/table]
and add the subroutine to at the bottom of the same module...
Code:
[table="width: 500"]
[tr]
	[td]Sub SumTimes()
  Dim T As Variant, Sum As Double
  On Error GoTo NoTotal
  For Each T In Array(txtCash1, txtCash2, txtCash3, txtCash4)
    T.BackColor = vbWhite
    If Len(T) Then Sum = Sum + CDate(T)
  Next
  txtGrandTotalCash1 = Application.Text(Sum, "[h]:mm")
  Exit Sub
NoTotal:
  T.BackColor = vbRed
  Resume Next
End Sub[/td]
[/tr]
[/table]
One thing I forgot to mention with the above code... it will turn the TextBox red for an in valid entry (text string, bad time value such as 1:99, and so on). Because of this, while you are entering your value, the TextBox will turn red when you enter the colon as, at that point, the entry is invalid... the color will become white again with the next value. Think about that for a moment... something like 1:2 will be recognized as a real time value. Why? Because Excel assumes 1:2 is really 1:02. Just thought I should mention all of this so you are not surprised when you implement my code.
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Where did you learn how to code all this stuff? I'd like to get more advanced.
I am the wrong person to ask about learning VBA as I am completely self-taught and grew into VBA from its predecessor programming languages BASIC and Visual Basic (compiled version). I starting my programming "career" in 1981 and have literally written code every day since. After 35+ years of programming, you kind of get familiar with the language and "advanced" concepts become almost second nature. One of my fellow contributors on this forum compiled a list of VB learning resources that may help you out though...

https://www.mrexcel.com/forum/excel...w-use-vbulletin-excel-2007-a.html#post4760720
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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