gpt4 book ai didi

VBA ByRef 参数类型不匹配

转载 作者:行者123 更新时间:2023-12-02 21:24:12 26 4
gpt4 key购买 nike

最初在我的主代码部分中,我有一个丑陋的 if 语句 - 尽管它会运行丑陋。我决定将其设为我要调用的函数,这导致我收到错误“编译错误:ByRef 参数类型不匹配”。我的假设是该函数需要正确引用,尽管我一直在阅读文档并且不明白为什么>.<

声明 ShiftValue 变量:
Dim ShiftValue As String<br/>
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)

所引用的任何单元格中的值要么为空,要么为字符串。

最佳答案

除了上面 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

关于VBA ByRef 参数类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com