在范围内但要留在列中的值搜索框

问题描述 投票:1回答:1

自5个小时以来,我一直在努力应对以下代码,我希望对此进行扩展。我只是想通过输入框模拟内置的ctrl + F搜索工具,添加该功能以在查找名称(字母而不是数字)时留在行/列(ActiveCell)中。

工作代码:

Sub Jarvis()

Dim sResult As String

  On Error Resume Next

  sResult = InputBox("Bitte Produktname angeben")

  If IsNumeric(sResult) Then 'row
    Cells(sResult, ActiveCell.Column).Select
  Else 'Select column - Here I want to add a way to search for names/values and to jump to them, while staying in the current column - Unfortunately didn't find the suiting command
    Cells(ActiveCell.Row, sResult).Select

  End If

End Sub

非工作代码:

Sub Jarvis2()

Dim sResult As String

  On Error Resume Next

  sResult = InputBox("Bitte Produktname angeben")

  If IsNumeric(sResult) Then 'row
    Cells(sResult, ActiveCell.Column).Select
  Else 'Select column - Here I want to add a way to search for names/values and to jump to them, while staying in the current column - Unfortunately didn't find the suiting command
    Columns("AW2:JZ5", sResult).Select

  End If

End Sub

任何专业人士都可以帮助我吗?

excel vba search row
1个回答
1
投票

谢谢您对PEH的帮助。

这是我的解决方法:

 Sub Jarvis()

    Dim word, cellRange, firstAddress, c, FindNext, BottomRight As String

    word = InputBox("insert search term", "")

    If word = "" Then Exit Sub

    cellRange = "aw2:ku5"
    'change search range

    With ActiveSheet.Range(cellRange)
        Set c = .Find(word)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                Range(c.Address, ActiveCell).Select
                    With Selection
                        BottomRight = .Cells(.Rows.Count, .Columns.Count).Select
                    End With
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
End Sub

如果任何专业人士找到更有效的方法,请更正它。

© www.soinside.com 2019 - 2024. All rights reserved.