我对 VBA 和编程非常陌生。我正在尝试以下操作:
- 单击命令按钮 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
最佳答案
去掉 Find
语句中的双引号:
Set cell = Selection.Find(What:=StringToFind, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
将其括在引号中意味着将其解释为文字字符串,而不是变量 StringToFind
中保存的变量值。
可能还有其他错误。我没有进一步测试你的代码。如果您需要亲自前往牢房,您可以使用
Application.GoTo 单元格
或者你可以使用
cell.Activate
关于vba - 查找特定行、工作表中输入框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280396/