Range.Find 在 VBA Excel 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28664035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Range.Find in VBA Excel
提问by Nilo85
I'm trying to perform a "find" in a Excel sheet with this instruction:
我正在尝试使用以下说明在 Excel 工作表中执行“查找”:
Set Found = Columns(2).Find(What:=value_to_find, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
but I get the error "Run-time error '13': Type mismatch".
但我收到错误“运行时错误‘13’:类型不匹配”。
If I use Range("A1:H1") instead of Columns(2) I don't get any errors, but I think they are both type Range.
如果我使用 Range("A1:H1") 而不是 Columns(2) 我不会收到任何错误,但我认为它们都是 Range 类型。
My aim is use a Range variable, at the start of the script I valorize it according to the user's choice.
我的目标是使用 Range 变量,在脚本开始时我根据用户的选择对其进行赋值。
回答by Nilo85
[RESOLVED] Many Thanks everyone, I resolved my question with the Ralph's support.
[已解决] 非常感谢大家,我在拉尔夫的支持下解决了我的问题。
The variable Found (in your code) must be of type variant. So, if you explicitly set Dim Found as variant then your code should work.
变量 Found(在您的代码中)必须是变体类型。因此,如果您明确地将 Dim Found 设置为变体,那么您的代码应该可以工作。
回答by whytheq
Exactly as @barrowc commented.
正如@barrowc 评论的那样。
This works although not sure of exact requirements:
虽然不确定确切的要求,但这是有效的:
Sub xxx()
Dim value_to_find As String
value_to_find = "fooBar"
Dim r As Range
Set r = ActiveCell.EntireColumn.Find( _
What:=value_to_find, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
MsgBox r.Address
End Sub