从另一个函数中调用VBA函数

时间:2010-11-19 15:07:29

标签: excel function vba return

下面是我编写的代码,但是我一直在向我添加评论的行提出问题,而且只是那行。我已经评论了所有其他线路并将其作为问题线隔离,但对于我的生活以及我所做的一小时或更长时间的研究,我无法弄清楚问题是什么。这可能是一个非常明显的一个,但我真的被卡住了,这让我发疯了。

无论如何,该代码用于获取包含移位时间和语言功能的数据范围,并显示在给定时间段内有多少具有特定语言的人可用(下面代码中的The_Time)

非常感谢任何帮助!

Function ReturnAvailability(The_Time As String, The_Info As Range)

Dim The_Lang As String
Dim The_Shift_Start As String
Dim The_Shift_End As String
Dim stGotIt As String
Dim stCell As Integer
Dim Counter As Integer

Counter = 0

For Each r In The_Info.Rows
    For Each c In r.Cells
        stCell = c.Value
        If InStr(stCell, "Eng") > 0 Then
            The_Lang = "Eng"
        ElseIf InStr(c, ":") > 0 Then
            stGotIt = StrReverse(c)
            stGotIt = Left(c, InStr(1, c, " ", vbTextCompare))
            The_Shift_End = StrReverse(Trim(stGotIt))
            stGotIt = Left(The_Shift, InStr(1, The_Shift, " ", vbTextCompare))
            The_Shift_Start = stGotIt
            stCell = ReturnAvailabilityEnglish(The_Time, The_Shift_Start, The_Shift_End) ' this is the line causing the error
        End If
    Next c
Next r

ReturnAvailability = Counter

End Function


Function ReturnAvailabilityEnglish(The_Time As String, The_Shift_Start As String, The_Shift_End As String)

Dim Time_Hour As Integer
Dim Time_Min As Integer
Dim Start_Hour As Integer
Dim Start_Min As Integer
Dim End_Hour As Integer
Dim End_Min As Integer
Dim Available As Integer

Available = 13

Time_Hour = CInt(Left(The_Time, 2))
Time_Min = CInt(Right(The_Time, 2))
Start_Hour = CInt(Left(The_Shift_Start, 2))
Start_Min = CInt(Right(The_Shift_Start, 2))
End_Hour = CInt(Left(The_Shift_End, 2))
End_Min = CInt(Right(The_Shift_End, 2))

If Start_Hour <= Time_Hour And Start_Min <= Time_Min Then
    If End_Hour > Time_Hour And End_Min > Time_Min Then
        Available = 1
    Else
        Available = 0
    End If
End If

ReturnAvailabilityEnglish = Available

End Function

谢谢, Darragh J

1 个答案:

答案 0 :(得分:1)

您已宣布

Dim stCell As Integer

这意味着此部分不起作用:

stCell = c.Value
If InStr(stCell, "Eng") > 0 Then

c.Value的赋值将失败,因为它包含文本,或者InStr(stCell,“Eng”)永远不会成立,因为范围内的所有单元格都是数字。

您缺少文字比较:

 If InStr(1, stCell, "Eng", vbTextCompare) > 0 Then

这也是一个问题,您需要添加一个支票,如图所示:

If The_Time = vbNullString Or The_Shift_Start = vbNullString _
    Or The_Shift_End = vbNullString Then
    Available = -1
Else

    Time_Hour = CInt(Left(The_Time, 2))
    Time_Min = CInt(Right(The_Time, 2))
    Start_Hour = CInt(Left(The_Shift_Start, 2))
    Start_Min = CInt(Right(The_Shift_Start, 2))
    End_Hour = CInt(Left(The_Shift_End, 2))
    End_Min = CInt(Right(The_Shift_End, 2))

    If Start_Hour <= Time_Hour And Start_Min <= Time_Min Then
        If End_Hour > Time_Hour And End_Min > Time_Min Then
            Available = 1
        Else
            Available = 0
        End If
    End If
End If
ReturnAvailabilityEnglish = Available

最后,最重要的是,你的函数将始终返回0,因为你在开始时将counter设置为0并且永远不会更新它。