I hope that this is a simple question. I have included sample subroutines so that others may benefit from the parts I have made work.
This is a simple time difference problem complicated by doing data entry on a Userform and wanting to see the calculated time difference as the numbers are changed. To calculate I have to convert the text to a number then back to text.
I have a Userform that I want to recalculate the difference between 2 dates and times anytime I make a change. I am using the textbox_change event to call a single procedure. I have made this work for numbers and dates by changing the text to values, then doing the math, and changing it back again.
It is not as bad as it sounds, but works on everthing but time values. Here is an example I moved to a separte module so I could test it using a simple principal = qty * price equation. It updates anytime either qty or price changes on the form, I copied it here to keep it simple.
Sub testPrinciple()
Dim txtQty, txtPrice, txtPrinciple
txtQty = 10 ‘ text because it is on a UserForm
txtPrice = 12.34
‘ I removed the .text from the line below to allow it to work as a stand alone subroutine
‘ txtPrinciple.Text = Format(Str(Val(Format(txtQty, “#,###”)) * Val(Format(txtPrice, “##.0000”))), “$#,###.0000”)
txtPrinciple = Format(Str(Val(Format(txtQty, “#,###”)) * Val(Format(txtPrice, “##.0000”))), “$#,###.0000”)
MsgBox txtPrinciple
End Sub
The difference between two dates works like this
Sub TestDateDiff()
Dim txtDaysIn, txtStartDate, txtEndDate
txtStartDate = #1/1/2001# ‘this would come from the UserForm
txtEndDate = #1/2/2001#
‘ txtDaysDiff.Text = Format(Str(Val(Format(txtEndDate, “dd/mm/yy”)) – Val(Format(txtStartDate, “dd/mm/yy”))), “#0”) ‘ Column AI
txtDaysDiff = Format(Str(Val(Format(txtEndDate, “dd/mm/yy”)) – Val(Format(txtStartDate, “dd/mm/yy”))), “#0”) ‘ Column AI
MsgBox txtDaysDiff
End Sub
But the time version does not work because the Value of the time difference below should be 1 hour and 10 minutes but Val() does not keep the decimal part and returns 0
Sub testtime()
Dim txtTimeDiff, txtStartTime, txtEndtime
txtStartTime = #10:36:53 AM#
txtStoptime = #11:46:53 AM#
txtTimeDiff = Format(Str( Val(Format(txtStopTime, “hh:mm”)) – Val(Format(txtStartTime, “hh:mm”)) ), “hh:mm”)
MsgBox txtTimeIn
End Sub
I am sure my problem is in using Val() on a time as it loses
Any and all help will be greatly appreciated.