在我的应用程序中,我调用一个函数并传递4个值来计算单元格地址:
nettowertadresse = searchAdress(Dateiname, CurrentSheet, "Nettowert", Range("A1:C60"))
功能:
Function searchAdress(inputworkbook As String, inputsheet As String, inputsearchtext As String, inputrange As Range) As Range
With Workbooks(inputworkbook).Sheets(inputsheet).Range(inputrange)
Set searchAdress = .Find(inputsearchtext, LookIn:=xlValues)
End With
End Function
现在问题是我得到错误1004“应用程序定义或对象定义错误”,我认为可能范围没有正确传递,因为调试器在跳转到函数时没有显示变量“inputrange”的值。请提供一些有关如何使此功能起作用的建议。感谢。
答案 0 :(得分:3)
您的函数searchAddress
声明inputrange as Range
。这意味着函数内的对象是Range
对象。
因此,您不应将其用作.Range(inputrange)
。而是尝试使用此代码,将其正确地视为Range
对象:
Function searchAdress(inputworkbook As String, inputsheet As String, inputsearchtext As String, inputrange As Range) As Range
With Workbooks(inputworkbook).Sheets(inputsheet)
Set searchAdress = inputrange.Find(inputsearchtext, LookIn:=xlValues)
End With
End Function
另请注意,您的代码中还有另一个调用此函数的错误。在为Set
变量指定返回值时,您需要使用关键字nettowertadresse
:
Set nettowertadresse = searchAdress(Dateiname, CurrentSheet, "Nettowert", Range("A1:BA2"))
否则您将遇到运行时错误'91',您在your follow-up question中提及。
答案 1 :(得分:2)
在函数中为rangeobject添加一个Range,你的函数如下所示:
With Workbooks(inputworkbook).Sheets(inputsheet).Range(Range("A1:C60"))
Set searchAdress = .Find(inputsearchtext, LookIn:=xlValues)
End With
你应该改变这个:
Function searchAdress(inputworkbook As String, inputsheet As String, inputsearchtext As String, inputrange As String) As Range
With Workbooks(inputworkbook).Sheets(inputsheet).Range(inputrange)
Set searchAdress = .Find(inputsearchtext, LookIn:=xlValues)
End With
End Function
然后将其传递给函数:
nettowertadresse = searchAdress(Dateiname, CurrentSheet, "Nettowert", "A1:C60")