2016-05-03 178 views
1

下面是我尝试运行的代码,我找不到任何错误,为什么我不能将字符串传递到函数与字符串参数?它不断告诉我ByRef参数类型不匹配。VBA 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功能retuns Varaint。例如来自函数GrabATTRS

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

call which causes ByRef error:

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

with string result example:

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

TY它的工作!,我不知道我必须首先将它们转换为字符串要处理 – Chrishadianto

+0

@Chrishadianto欢迎你!是的,如果你传递参数'ByRef',那么参数的类型必须是'String'(而不是'Split'返回的'Variant')。 – dee