VBA。查找dynamic更改参数

我有一个使用.find方法的VBA函数。 在我的函数中,我想改变xlPart或xlWhole之间的LookAt:参数。 我尝试使用参数variablesmatchvalue并传入“xlPart”或“xlWhole”。 它没有工作。

有没有办法改变LookAt:参数与我传入我的函数的variables?

Public Function getfield(matchvalue as string) GetRowNumber = Cells.Find(What:="Cat", After:=ActiveCell, LookIn:=xlValues, LookAt:= _ matchvalue, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , SearchFormat:=False).Activate End Function 

不知道你是如何分裂线条,使你无法实现你的目标。

试试:

 Public Function getfield(matchvalue as string) GetRowNumber = Cells.Find(What:="Cat", After:=ActiveCell, LookIn:=xlValues, _ LookAt:=matchvalue, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , SearchFormat:=False).Activate End Function 

阅读。 .Find文档,LookAt值应该是Variant Type。 阅读以下链接。

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel

目前还不清楚你的函数是想返回什么,但是我可能会这样处理:

 Public Function getfield(Optional matchWholeValue as Boolean = True) GetRowNumber = Cells.Find(What:="Cat", After:=ActiveCell, _ LookIn:=xlValues, _ LookAt:= IIf(matchWholeValue, xlWhole, xlPart), _ SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Row End Function 

匹配整个值的使用:

  getfield() 

并部分匹配:

  getfield(False) 

尝试:

 Public Function GetRowNumber(byval matchvalue as string, optional byval LookAtWhole = True) as long Dim Result as range Set Result = Activesheet.Cells.Find(What:=matchvalue After:=ActiveCell, LookIn:=xlvalues, LookAt:=iif(LookAtWhole,xlwhole,xlpart), SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , SearchFormat:=False) If not (Result is nothing) then Getrownumber = result.row Else Getrownumber = 0 End if End Function 

该function在活动工作表的单元上执行FIND方法。 如果条件没有返回结果,则返回0,否则返回行号。 你可以添加更多的参数。

希望它能起作用,而且是你想要的?