VBA ByRef Argument Type Mismatch string into string

Posted

技术标签:

【中文标题】VBA ByRef Argument Type Mismatch string into string【英文标题】: 【发布时间】:2016-05-03 07:50:17 【问题描述】:

这是我尝试运行的代码,我找不到任何问题,为什么我不能将 String 传递给带有 String 参数的函数?它一直告诉我 ByRef 参数类型不匹配。

一直在尝试有关此的其他答案,但我仍然找不到解决方案。希望能在这里得到一点帮助。

主要程序:

Sub Macro()
Dim test As String
Dim arr() As String
Dim CompiledText As String
Dim temporary As String
Dim i As Long
Dim ab As String
Dim marker As Long
market = 0
test = Range("A1").Value
arr = Split(test, Chr(10))
test = ""
CompiledText = ""
For i = 0 To UBound(arr)
If (Left(arr(i), 1) <> "#") Then
    If (Trim(test) <> "") Then
        test = test & vbCrLf
    End If
    test = test & arr(i)
    If (InStr(arr(i), "ATTRS(") <> 0) Then
        If (CompiledText <> "") Then
            CompiledText = CompiledText & vbCrLf
        End If
        temporary = arr(i)
        CompiledText = CompiledText & GrabATTRS(CStr(temporary))
    ElseIf (InStr(arr(i), "ATTRN(") <> 0) Then
        If (CompiledText <> "") Then
            CompiledText = CompiledText & vbCrLf
        End If
        temporary = arr(i)
        CompiledText = CompiledText & GrabATTRN(CStr(temporary))
    ElseIf (InStr(arr(i), "DB(") <> 0) Then
        If (CompiledText <> "") Then
            CompiledText = CompiledText & vbCrLf
        End If
        temporary = arr(i)
        CompiledText = CompiledText & GrabDB(CStr(temporary))
    End If
Else
    If (marker <> i - 1) Then
        arr(marker) = arr(marker) & vbCrLf & CompiledText
        CompiledText = ""
    End If
    marker = i
End If
Next i
test = ""
For i = 0 To UBound(arr)
If (i > 0) Then
test = test & vbCrLf
End If
test = test & arr(i)
Next i

Range("B1").Value = test
End Sub

被调用的函数不能正常工作:

Function GrabATTRS(ab As String) As String
Dim temp As String
Dim dimension As String
Dim attrib As String
temp = Split(Split(ab, "ATTRS(")(1), ")")(0)
dimension = onlyChars(Split(temp, ",")(0))
attib = onlyChar(Split(temp, ",")(UBound(Split(temp, ",")) - 1))
GrabATTRS = "#From dimension " & dimension & " pointing to " & attrib
End Function

Function GrabATTRN(ab As String) As String
Dim temp As String
Dim dimension As String
Dim attrib As String
temp = Split(Split(ab, "ATTRN(")(1), ")")(0)
dimension = onlyChars(Split(temp, ",")(0))
attib = onlyChar(Split(temp, ",")(UBound(Split(temp, ",")) - 1))
GrabATTRN = "#From dimension " & dimension & " pointing to " & attrib
End Function

Function GrabDB(ab As String) As String
Dim temp As String
Dim dimension As String
Dim attrib As String
temp = Split(Split(ab, "DB(")(1), ")")(0)
dimension = onlyChars(Split(temp, ",")(0))
GrabDB = "#From " & dimension & " Cube"
End Function

这一项功能可以跳过检查,因为它运行良好

Function onlyChars(S As String) As String
    Dim i As Integer
    retval = ""
    For i = 1 To Len(S)
        If Mid(S, i, 1) <> "'" Then
            retval = retval + Mid(S, i, 1)
        End If
    Next i
    onlyChars = retval
End Function


Option Explicit

【问题讨论】:

【参考方案1】:

Split 函数返回Varaint。示例例如来自函数GrabATTRS

Split 的结果可以放入字符串变量中,然后将ByRef 传递给onlyChars

导致 ByRef 错误的调用:

dimension = onlyChars(Split(temp, ",")(0))

字符串结果示例:

Dim result As String
result = Split(temp, ",")(0)
dimension = onlyChars(result)

【讨论】:

它正在工作!,我不知道我必须先将它们放入字符串中才能进行处理 @Chrishadianto 欢迎您!是的,如果您传递参数ByRef,则参数类型必须是String(而不是Split 返回的Variant)。

以上是关于VBA ByRef Argument Type Mismatch string into string的主要内容,如果未能解决你的问题,请参考以下文章

VBA:私有子中的编译错误 ByRef 参数类型不匹配

ByRef 参数类型不匹配 - Excel VBA

VBA中ByVal和 ByRef有啥区别?

在 VBA Excel 2007 中传递变体 ByRef

VBA 间歇性 ByRef 错误 - 格式函数

从被调用的函数调用函数时 VBA byref 参数类型不匹配