2013-06-24 28 views
2

我对VBA和编程非常陌生。我试图如下:在特定行中查找inputbox的值,工作表

  • 单击命令按钮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 
    

回答

1

摆脱在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

或者你可以使用

cell.Activate

1

如果唯一的目标就是激活细胞,并查看它,你所要做的是:

Selection.Find(what:="yourvalue").Activate 

所以,而不是设置小区=选择的值,使用与上述线

相关问题