VBA Userform calculations on Times (Start time - Finish Time = Total Hrs)

kitsa

Board Regular
Joined
Mar 4, 2016
Messages
66
Hi,
I'm trying to build a userform to calculate times to work out employee timesheet on how many hrs they have done e.g. Start time 9:00am to 5:00pm = 8hrs but I'm unable to get this as it's not calculating the two "times"
Is anyone able to help me with my VBA?

VBA Code:
TextBox3 = Int((Val(TextBox2.Value) - Val(TextBox1.Value)) * 24)

Full code of my userform
Code:
Private Sub cmdcalculate_Click()

TextBox3 = Int((Val(TextBox2.Value) - Val(TextBox1.Value)) * 24)
 

End Sub

Private Sub cmdclearinfo_Click()

 TextBox1.Value = ""
 TextBox2.Value = ""
 TextBox3.Value = ""
 
 
End Sub

Private Sub cmdexist_Click()
  Unload UserForm1
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
  
   If Not Me.TextBox1 Like "??:??" Then
     MsgBox "Please use format 'hh:mm'"
     Cancel = True
     Exit Sub
   Else
  
  End If
    
   myVar = Application.WorksheetFunction.Text(Me.TextBox1, "hh:mm am/pm")
  
   Me.TextBox1 = myVar
  
End Sub

Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not Me.TextBox2 Like "??:??" Then
     MsgBox "Please use format 'hh:mm'"
     Cancel = True
     Exit Sub
   Else
  
  End If
    
   myVar = Application.WorksheetFunction.Text(Me.TextBox2, "hh:mm am/pm")
  
   Me.TextBox2 = myVar
End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    32.3 KB · Views: 8

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

NoSparks

Well-known Member
Joined
Mar 15, 2013
Messages
1,047
Office Version
  1. 2010
Platform
  1. Windows
try
VBA Code:
     TextBox3 = (CDate(Me.TextBox2) - CDate(Me.TextBox1)) * 24
 

Watch MrExcel Video

Forum statistics

Threads
1,123,456
Messages
5,601,772
Members
414,472
Latest member
Chris_1990

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
Top