vba 我想使用命令按钮在活动工作表中的 textbox2 中搜索/查找值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14619694/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:25:13  来源:igfitidea点击:

I want to search/find value in textbox2 in an active sheet using command button

excel-vbavbaexcel

提问by user2022143

I want to search/find value in textbox2 in an active sheet using command button

我想使用命令按钮在活动工作表中的 textbox2 中搜索/查找值

Here is my code:

这是我的代码:

Dim ws As Worksheet
Set ws = Worksheets("FSS-TEM-00025")
Dim FindString As String
    Dim Rng As Range    
    FindString = Me.TextBox2.Value
    If Trim(FindString) <> "" Then
        With ws.Range("A1:Z1048576")
            'Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)'
             Set Rng = ws.Cells.Find(What:=FindString, SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If


     Unload Me

回答by Siddharth Rout

Try this. This works for me

尝试这个。这对我有用

Option Explicit

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim FindString As String
    Dim Rng As Range

    Set ws = ThisWorkbook.Worksheets("FSS-TEM-00025")

    FindString = TextBox2.Value

    If Trim(FindString) <> "" Then
        Set Rng = ws.Cells.Find( _
                         What:=FindString, _
                         LookIn:=xlValues, _
                         LookAt:=xlPart, _
                         SearchOrder:=xlByRows, _
                         SearchDirection:=xlNext, _
                         MatchCase:=False, _
                         SearchFormat:=False)

        If Not Rng Is Nothing Then
            Application.Goto Rng, True
        Else
            MsgBox "Nothing found"
        End If
    End If
End Sub

Change LookAt:=xlPartto LookAt:=xlWholeif you are trying to find a complete match.

如果您要查找完整匹配项,请更改LookAt:=xlPartLookAt:=xlWhole

More on .Findhere.

更多在.Find这里