首页 > 解决方案 > 在 VBA 用户表单中,当您输入开始时间和结束时间时,如何在第三个文本框中填充时差

问题描述

我正在准备一个 VBA 用户表单我有一个用于开始时间的文本框和其他用于结束时间的文本框。现在在第三个文本框中我想要将上述两个时间之间的差异作为自动填充的条目。但是我的第三个文本框没有显示时间区别。我发布我的代码以供参考:

Private Sub TxtBxHrs_Change()


Dim sDate As Date, eDate As Date


'Start Date
sDate = DateValue(Me.TxtBxStarttime.Value)

'End Date
eDate = DateValue(Me.TxtBxEndtime.Value)

   
Me.TxtBxHrs.Value = DateDiff("h", sDate, eDate)
End Sub

标签: excelvba

解决方案


DateValued会给你Date(整数部分)的日期部分。您需要TimeValueDateValue. 尝试

sDate = DateValue(Me.TxtBxStarttime.Value) + TimeValue(Me.TxtBxStarttime.Value)

推荐阅读