函数返回MsgBox 10次?

时间:2015-12-21 14:11:06

标签: excel vba excel-vba

Excelguru上找到了一个功能,我改变了一些内容并编辑了一些内容。我们的想法是使用它来记录工作时间和分钟。

有一件事我不明白:如果我在列reff中键入错误的时间,我会得到一个错误的消息,但它不会消失,除非我点击它10次。我看不出我做错了什么。整个代码已发布,我很感激任何帮助。

将他的功能用作表格中公式的一部分,如:;WITH cte AS (SELECT Ca.dates, C, B FROM (SELECT DISTINCT C, B FROM fact) A CROSS JOIN calendar Ca) SELECT A.dates, A.B, A.C, Isnull(F.value,0) as value FROM cte A LEFT OUTER JOIN fact F ON A.dates = f.dates AND A.C = F.C AND A.B = F.B

TimeValue($E2;$F2;"16:00";"18:00";B2;9;C2)

表中的子

Function TimeValue(FromTime As String, ToTime As String, StartTime As String, StopTime As String, Optional Weekday As String, Optional Daynr As Integer, Optional Holiday As String)
    Dim x As Long
    Dim F As Double
    Dim T As Double
    Dim Start As Double
    Dim Stopp As Double
    Dim Min As Long
    Dim Day As Integer
    Dim OverMid As Boolean

    Select Case LCase(Weekday)
        Case "mandag"
            Day = 1
        Case "tirsdag"
            Day = 2
        Case "onsdag"
            Day = 3
        Case "torsdag"
            Day = 4
        Case "fredag"
            Day = 5
        Case "lordag"
            Day = 6
        Case "sondag"
            Day = 7
        Case "x"
            Day = 8
        Case Else
            Day = 0
    End Select

    OverMid = False

    If LCase(Holiday) = "x" Then Day = 8

    If Len(FromTime) = 0 Or Len(ToTime) = 0 Then
        Exit Function
    End If

    If Len(FromTime) <> 5 Then
        MsgBox ("Use format TT:MM - From time is wrong:" & FromTime)
        Exit Function
    End If

    If Len(ToTime) <> 5 Then
        MsgBox ("Use format TT:MM - To time is wrong:" & ToTime)
        Exit Function
    End If

    F = Val(Left(FromTime, 2)) * 60 + Val(Right(FromTime, 2))
    T = Val(Left(ToTime, 2)) * 60 + Val(Right(ToTime, 2))

    Start = Val(Left(StartTime, 2)) * 60 + Val(Right(StartTime, 2))
    Stopp = Val(Left(StopTime, 2)) * 60 + Val(Right(StopTime, 2))

    If T = 0 Then T = 24 * 60
    If T < F Then
        T = T + 24 * 60
        OverMid = True
    End If

    If Stopp = 0 Then Stopp = 24 * 60

    For x = F + 1 To T
        If x > Start And x <= Stopp Then
            Min = Min + 1
        End If
    Next x

    If OverMid = True Then
        For x = 0 To Val(Left(ToTime, 2)) * 60 + Val(Right(ToTime, 2))
            If x > Start And x <= Stopp Then
                Min = Min + 1
            End If
        Next x
    End If

    'If weekday is set, equal to day
    If Daynr <> 0 Then
        If Daynr <> 9 Then
            If Day <> Daynr Then Min = 0
        End If

        If Daynr = 9 And (Day > 5) Then
            Min = 0
        End If
    End If

    TimeValue = Min / 60

End Function

1 个答案:

答案 0 :(得分:1)

消息框实际上不属于UDF(VBA函数意味着用作电子表格函数)。

您可以使用以下代码代替消息框:

If Len(FromTime) <> 5 Then
        TimeValue = "Error! Use format TT:MM - From time is wrong:" & FromTime
        Exit Function

或者也许:

If Len(FromTime) <> 5 Then
       TimeValue = CVErr(xlErrValue)
       Exit Function

稍后会导致#VALUE!显示在单元格中。在电子表格中包含足够的文档,以便用户可以解释此类错误值。