2

我有三个单元格 IN 、 OUT 和 OverTime 都格式化为 [h]:mm,OT 单元格有这个论坛,

=ROUND(IF(((D10-C10)+(D11-C11))*24>7,((D10-C10)+(D11-C11))*24-7,0)/24*96,0)/96 

计算 OT 到 1/4 小时

8 C D E F 
9   IN      OUT      O/T  C/T
10 7:30 AM 12:15 PM 1:45
11 1:00 PM 5:00 PM

   当员工当天下班时,我希望运行以下代码;

Dim CT As Date
Title = "Add to CompTime from OverTime"
If Range("E10") > 0 Then
CT = InputBox("Add Hours to CompTime?", Title)
If CT > 0 Then Range("F10").Value = ("E10" - CT)
Else: Range("F10").Value = " "
End If
End Sub

一切似乎都有效,除了;如果 CT > 0 Then Range("F10").Value = ("E10" - CT) 我知道这是格式问题,但我无法解决问题。

4

1 回答 1

0

始终尝试对范围进行显式引用,无论它们是在工作表还是模块上。使用对象的适当属性 - 在您的情况下,您有一个不合格E10的范围。假设您在工作表 1:

Option Explicit
'--Beginning of your Subroutine...
Dim Title as String '-- assuming
Dim CT As Date '-- are you sure you want to have a date here?

Title = "Add to CompTime from OverTime"

  If Sheets(1).Range("E10").Value > 0 Then '-- assuming it's a valid date here...
    CT = InputBox("Add Hours to CompTime?", Title)
    '-- assume your CT = 2:45 and OT = 75:30
    '-- use the following as mentioned in my comment
    If CT > 0 Then 
       Application.Text( Sheets(1).Range("F10").Value ,"[h]:mm")  = 
       Application.Text(Sheets(1).Range("E10").Value, "[h]:mm") - Application.Text(CT, "[h]:mm") 
    Else
       Sheets(1).Range("F10").Value = " "
    End If
  End If
End Sub

PS:如果您要计算小时数,您可能只使用 VBA 进行整个计算.. =)

于 2013-01-28T03:07:23.247 回答