vba 查找特定行、工作表中输入框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17280396/
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
Find value of inputbox in specific row, sheet
提问by halcyon27
I am very new to VBA and programming. I am attempting the following:
我对 VBA 和编程很陌生。我正在尝试以下操作:
- Click command button btnUpdateEntry to launch an inputbox
- Search for value of inputbox in the sheet "output" in column A
- Go to first location where value found
Display message if value not found
Private Sub btnUpdateEntry_Click() Dim StringToFind As String StringToFind = Application.InputBox("Enter string to find", "Find string") Worksheets("output").Activate ActiveSheet.Range("A:A").Select Set cell = Selection.Find(What:="StringToFind", After:=ActiveCell, _ LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) If cell Is Nothing Then Worksheets("input").Activate MsgBox "String not found" End If End Sub
- 单击命令按钮 btnUpdateEntry 以启动输入框
- 在A列的“输出”表中搜索输入框的值
- 转到找到值的第一个位置
如果未找到值则显示消息
Private Sub btnUpdateEntry_Click() Dim StringToFind As String StringToFind = Application.InputBox("Enter string to find", "Find string") Worksheets("output").Activate ActiveSheet.Range("A:A").Select Set cell = Selection.Find(What:="StringToFind", After:=ActiveCell, _ LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) If cell Is Nothing Then Worksheets("input").Activate MsgBox "String not found" End If End Sub
采纳答案by David Zemens
Get rid of the double quotes in your Find
statement:
去掉Find
语句中的双引号:
Set cell = Selection.Find(What:=StringToFind, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Enclosing this in quotes means to interpret it as a literal string, rather than the variable value held in your variable, StringToFind
.
将其括在引号中意味着将其解释为文字字符串,而不是变量StringToFind
.
There may be other errors. i did not test your code any further. If you need to physically go to the cell, you could use
可能还有其他错误。我没有进一步测试您的代码。如果你需要亲自去牢房,你可以使用
Application.GoTo cell
Application.GoTo cell
Or you could use
或者你可以使用
cell.Activate
cell.Activate
回答by Acantud
If the only goal is to activate that cell and view it, all you have to do is:
如果唯一的目标是激活该单元格并查看它,您所要做的就是:
Selection.Find(what:="yourvalue").Activate
So instead of setting cell = the value of the selection, use that above line
因此,不要设置单元格 = 选择的值,而是使用上面的行