Excel VBA中的ByRef参数types不匹配

我正在使用VBA。 我写了一个用户定义函数,它接受一个string ,处理它并返回一个干净的string 。 我不确定它有什么问题。 我无法调用它并要求它处理我的string并将其返回。 我认为我定义或返回它的方式有一个错误。

 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 "[AZ, az, 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 

我用这个function

 Worksheets(data_sheet).Range("C2").Value = ProcessString(last_name) 

姓氏是一个stringvariables,通常看起来像这个姓氏Lastname***** ,我试图删除它后面的所有星星。 有没有星星返回Lastname

我收到Compile error: ByRef arugment type mismatch当我试图运行这个时, 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) 

我怀疑你没有在调用者中正确设置last_name

使用Worksheets(data_sheet).Range("C2").Value = ProcessString(last_name)

这只会工作,如果last_name是一个string,即

 Dim last_name as String 

出现在调用者的某处。

原因是VBA通过引用默认传递variables这意味着数据types必须在调用者和被调用者之间完全匹配。

两个修复:

1)将你的函数改为Public Function ProcessString(ByVal input_string As String) As String

2)在使用它之前,在调用者中放入Dim last_name As String

(1)工作,因为对于ByVal ,传递给将强制它到正确的数据types的函数的input_string的副本。 由于该函数不能修改调用者中的variables,因此也导致更好的程序稳定性。

我不知道为什么,但如果要将variables(作为variables)传递到其他过程或函数中,则分别声明variables非常重要。

例如,有一个程序会对数据进行一些操作:根据ID返回零件号和数量信息。 ID作为常量值,其他两个参数是variables。

 Public Sub GetPNQty(ByVal ID As String, PartNumber As String, Quantity As Long) 

下一个主代码给了我一个“ByRef参数不匹配”:

 Sub KittingScan() Dim BoxPN As String Dim BoxQty, BoxKitQty As Long Call GetPNQty(InputBox("Enter ID:"), BoxPN, BoxQty) End sub 

而下一个也在工作:

 Sub KittingScan() Dim BoxPN As String Dim BoxQty As Long Dim BoxKitQty As Long Call GetPNQty(InputBox("Enter ID:"), BoxPN, BoxQty) End sub 

我改变了一些与Option Explicit一起工作的东西,并且代码对包含"abc.123"的单元格运行正常,后者返回"abc.12," 。 没有编译错误。

 Option Explicit ' This is new Public Function ProcessString(input_string As String) As String ' The temp string used throughout the function Dim temp_string As String Dim i As Integer ' This is new Dim return_string As String ' This is new For i = 1 To Len(input_string) temp_string = Mid(input_string, i, 1) If temp_string Like "[AZ, az, 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 

我会build议你发布更多的相关代码(即调用这个函数)。 你已经说过,last_name是一个string,但似乎并非如此。 逐行通过你的代码,并确保这是事实。

在一次一个字符的循环中,一个字符是一个可行的方法,没有必要。 VBA内置了这样的function:

 Public Function ProcessString(input_string As String) As String ProcessString=Replace(input_string,"*","") End Function