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

kitsa

Board Regular
Joined
Mar 4, 2016
Messages
111
Office Version
  1. 365
  2. 2016
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: 37

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
try
VBA Code:
     TextBox3 = (CDate(Me.TextBox2) - CDate(Me.TextBox1)) * 24
 
Upvote 0

Forum statistics

Threads
1,214,608
Messages
6,120,500
Members
448,968
Latest member
screechyboy79

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