Excel 从轮班时间中扣除30分钟

Excel 从轮班时间中扣除30分钟,excel,vba,Excel,Vba,您能否给我一个代码,从轮班时间(例如5:00:00)中扣除“00:30:00”,并存储在另一个变量中。 换档时间将从输入框中获取。 加法给出正确答案(5:30:00),其中as扣除给出0.528999等 If shin2 <> "" Then intime2 = TimeValue(shin2) + TimeValue("00:30:00") MsgBox intime2 intime22 = TimeValue(shin2) - TimeValue("00:30:0

您能否给我一个代码,从轮班时间(例如5:00:00)中扣除“00:30:00”,并存储在另一个变量中。 换档时间将从输入框中获取。 加法给出正确答案(5:30:00),其中as扣除给出0.528999等

  If shin2 <> "" Then
  intime2 = TimeValue(shin2) + TimeValue("00:30:00")
  MsgBox intime2
  intime22 = TimeValue(shin2) - TimeValue("00:30:00")
  MsgBox intime22
  End If
如果是“2”,则
intime2=时间值(shin2)+时间值(“00:30:00”)
MsgBox intime2
intime22=时间值(shin2)-时间值(“00:30:00”)
MsgBox intime22
如果结束
亲切问候

请考虑:

Sub SHIFTTIME()
    Dim t As Date, s As String
    s = Application.InputBox(Prompt:="Enter time as hh:mm:ss", Type:=2)
    t = TimeValue(s) - TimeSerial(0, 30, 0)
    MsgBox t
End Sub

我检查了你的代码,我觉得还可以

Sub TimeValueTest()

Dim shin2 As String
Dim intime22 As Double, intime2 As Double


 shin2 = InputBox("Enter time in hh:mm:ss format")

 If shin2 <> "" Then
  intime2 = TimeValue(shin2) + TimeValue("00:30:00")

  Debug.Print "intime2"
  Debug.Print "value: " & intime2
  Debug.Print "time: " & Format(intime2, "hh:mm:ss")
  Debug.Print "----"

  intime22 = TimeValue(shin2) - TimeValue("00:30:00")

  Debug.Print "intime22"
  Debug.Print "value: " & intime22
  Debug.Print "time: " & Format(intime22, "hh:mm:ss")
  End If


End Sub

在bonnet TimeValue下,将时间字符串转换为十进制数,因此我怀疑您奇怪的结果可能与您的变量及其声明方式有关。

您能粘贴当前使用的代码吗?感谢朋友的承诺。下面是+工作正常的代码,-ve给出了错误的答案。如果shin2“”,那么intime2=TimeValue(shin2)+TimeValue(“00:30:00”)MsgBox intime2 intime22=TimeValue(shin2)-TimeValue(“00:30:00”)MsgBox intime22 End如果您声明了变量?intime2和intime22声明为什么?
intime2
value: 0.229166666666667
time: 05:30:00
----
intime22
value: 0.1875
time: 04:30:00