最初在我的主要代码部分,我有一个丑陋的if语句 - 虽然丑陋但它会运行。我决定让它成为一个我会调用的函数,这导致我得到一个错误" 编译错误:ByRef参数类型不匹配"。我的假设是函数需要被正确引用,尽管我一直在阅读文档而无法理解>。<。<。<。<
声明ShiftValue变量:
Dim ShiftValue As String
ShiftValue = LCase(Sheets("Raw_Rota").Cells(Counter, "C").Value)
功能内容和声明:
Function ShiftCompare(ByRef ShiftValue As String)
If StrComp(ShiftValue, "am", vbTextCompare) = 0 Then
Call IncAMs(AMs) 'this function increments the variable by 1.
Call Inc(Counter)
ElseIf StrComp(ShiftValue, "pm", vbTextCompare) = 0 Then
Call IncPMs(PMs)
Call Inc(Counter)
ElseIf StrComp(ShiftValue, "days", vbTextCompare) = 0 Then
Call IncDays(Days)
Call Inc(Counter)
ElseIf StrComp(ShiftValue, "leave", vbTextCompare) = 0 Then
Call IncLeave(Leave)
Call Inc(Counter)
Else 'If the string doesn't compare to the above values tally it as unknown
Call IncUnknown(Unknown)
Call Inc(Counter)
End If
End Function
更新
我的函数调用位于If部分的Else部分,如下所示:
If X
"'Do stuff..."
Else
Call ShiftCompare(ShiftValue)
EndIf
在功能线上引发错误:
Function ShiftCompare(ByVal ShiftValue As String)
被引用的任何单元格中的值为空或字符串。
答案 0 :(得分:1)
除了Vityata的上述答案之外,请考虑使用Select语句来避免所有ElseIf
语句。它运行得更快。
Option Compare Text
Public Sub ShiftCompare(ByVal ShiftValue As String)
Select Case True
Case StrComp(ShiftValue, "am") = 0:
Call IncAMs(AMs) 'this function increments the variable by 1.
Call Inc(Counter)
Case StrComp(ShiftValue, "pm") = 0:
Call IncPMs(PMs)
Call Inc(Counter)
Case StrComp(ShiftValue, "days") = 0:
Call IncDays(Days)
Call Inc(Counter)
Case StrComp(ShiftValue, "leave") = 0:
Call IncLeave(Leave)
Call Inc(Counter)
Case Else: 'If the string doesn't compare to the above values tally it as unknown
Call IncUnknown(Unknown)
Call Inc(Counter)
End Select
End Sub
答案 1 :(得分:0)
调用函数时参数的值不是字符串。检查ShiftValue
的值,然后重试。如果您想确保它是一个字符串,请将其传递给Call ShiftCompare("MyStringValue")
函数的想法是它返回一个值是好的。
你的不回来。将Function
更改为Sub
。