我正在使用VBA。我寫了一個用戶定義函數,它需要一個string
,處理它並返回一個清理的string
。我不確定它有什麼問題。我無法調用它並要求它處理我的字符串並將其返回。我認爲我定義或返回它的方式存在錯誤。Excel中的ByRef參數類型不匹配VBA
Public Function ProcessString(input_string As String) As String
' The temp string used throughout the function
Dim temp_string As String
For i = 1 To Len(input_string)
temp_string = Mid(input_string, i, 1)
If temp_string Like "[A-Z, a-z, 0-9, :, -]" Then
return_string = return_string & temp_string
End If
Next i
return_string = Mid(return_string, 1, (Len(return_string) - 1))
ProcessString = return_string & ", "
End Function
而且我用這個函數像這樣
Worksheets(data_sheet).Range("C2").Value = ProcessString(last_name)
姓是一個字符串變量,通常看起來像這樣Lastname*****
,我試圖刪除所有它背後的明星。沒有星星,請返回Lastname
。
我收到Compile error: ByRef arugment type mismatch
當我試圖運行這個。我使用Windows XP與Office 2003
編輯:我加了我的代碼的基本結構調查,我有大約20類似的代碼行。這樣做的每個字段我需要同樣的事情。
Private Sub CommandButton2_Click()
' In my original production code I have a chain of these
' Like this Dim last_name, first_name, street, apt, city, state, zip As String
Dim last_name As String
' I get the last name from a fixed position of my file. Because I am
' processing it from another source which I copied and pasted into excel
last_name = Mid(Range("A4").Value, 20, 13)
' Insert the data into the corresponding fields in the database worksheet
Worksheets(data_sheet).Range("C2").Value = ProcessString(last_name)
在哪條線上錯誤標誌? – Gaffi
當我打電話時,它在線上閃現。 '工作表(data_sheet).Range(「C2」)。Value = ProcessString(last_name)' – George