以用户定义的范围作为inputfunction
我在Excel中对VBA来说是比较新的。 我想编写一个自定义函数,它将用户定义的范围作为input,然后在该范围内search特定的单词。 到目前为止,下面的代码工作,但它只在预定义的范围“a2:g2”search,但我想要的东西,将更改为“a3:g3”等等,当用户填充:
Function findme() With Worksheets("Filtered").Range("a2:g2") Set c = .Find("XXXX", LookIn:=xlValues) If Not c Is Nothing Then findme = "Match Found" Else: findme = "No match" End If End With
我认为这应该工作,但事实并非如此。 它返回“VALUE”
Function findme(myrange as range) With Worksheets("Filtered").Range(myrange) Set c = .Find("XXXX", LookIn:=xlValues) If Not c Is Nothing Then findme = "Match Found" Else: findme = "No match" End If End With
myrange不会作为地址/string传递给函数,而是实际的范围。 在代码中使用它的方式( Worksheets("Filtered").Range(myrange)
)需要一个地址。 你可以保留你的代码,并使用myrange.address
而不仅仅是myrange
,或者你可以使用variables是你需要继续的事实。
Function findme(myrange as range) as string With myrange Set c = .Find("XXXX", LookIn:=xlValues) If Not c Is Nothing Then findme = "Match Found" Else: findme = "No match" End If End With