2010-11-19 115 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

相關問題