vba - 查找特定行、工作表中输入框的值

标签 vba

我对 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/

相关文章:

vba - 基于范围 VBA 的过滤列

sql-server - 打开的对象上的运行时错误 3704

vba - 使用 vba 将数据从 Excel 复制到 powerpoint

vba - 如何优化在大量记录上运行的宏?

ms-access - 如何在 MS Access 中返回带有 form_error 事件的描述

Excel VBA datediff 一起计算月、日和月?

vba - 获取字符串的第 n 行

vba - Excel Shape.TextFrame.Characters.Insert 在 Excel 2007 上是否损坏?

excel - 当 Evaluate Match 找不到它的条件时显示错误

excel - 有没有办法简化这段代码?该代码在 3 张纸上重复。