2010-11-19 61 views
0

下面是我编写的代码,但是我一直对我添加注释的行感到不解,并且只是该行。我已经评论了所有其他的路线,并将其隔离为问题路线,但是对于我的生活以及我已经完成的一小时或更多的研究,我无法弄清楚问题所在。这可能是一个非常明显的,但我真的被卡住了,这让我疯狂。从另一个函数内部调用VBA函数

不管怎样,代码是用来取含换挡时间和语言能力数据的范围,并显示有多少人有特定的语言如何都可以在给定的时间段(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 

感谢, 达拉赫Ĵ

+0

哪一行导致错误?它是编译错误还是运行时错误? – 2010-11-19 15:15:13

+0

对不起,评论加入了 – 2010-11-19 17:57:02

+0

什么是错误? – BenV 2010-11-19 18:24:44

回答

1

您已经声明

Dim stCell As Integer 

这意味着,这部分不能工作:

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

无论c.Value的分配将失败,因为它包含文本,或InStr(stCell,“Eng”)永远不会是真的,因为e范围内的所有单元都是数字。

你缺少文本比较:

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,因为您在开始时将计数器设置为0,并且从不更新它。

+0

好吧,我刚刚解决了这个问题,但它没有做出任何改变。我仍然只获得#VALUE!在我的工作表中。感谢您指出,虽然!任何其他想法......? – 2010-11-19 17:54:28

+0

添加了更多注释。 – Fionnuala 2010-11-19 18:31:45

+0

感谢这些笔记,我实际上遇到了以前的比较问题,这是在尝试纠正这个问题时发现的,因此我将它们剥离出来。我已经将它们添加回来了,它们不会引起任何问题,并且我意识到始终返回0的问题,这并不重要,因为我目前没有收到任何有效值。谢谢你的支票。但是,这仍然没有解决问题。 – 2010-11-19 18:44:57

相关问题