2016-05-03 175 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