根据单元格的内容进行搜索

时间:2015-09-03 15:29:38

标签: excel vba excel-vba

我设置了一个命令按钮,并为其分配了一个宏。我需要按钮来获取单元格B2的内容,然后在下一页的A列中搜索它。这是我的代码。正如您所看到的,当我录制宏时,它正在寻找那里的文字文本。如何搜索输入B2的内容?

Sub Button3_Click()

    Range("B2").Select
    Selection.Copy
    Sheets("Sheet3").Select
    Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
End Sub

2 个答案:

答案 0 :(得分:2)

这可能对你所需要的东西有点过分,但我总是相信错误检查以及使用完整而灵活的代码,所以这就是你所要求的,带有评论:

Sub btnFindText()

    'Declare variables
    Dim wb As Workbook          'Used to store and reference the ActiveWorkbook
    Dim wsActive As Worksheet   'Used to store and reference the ActiveSheet (the sheet containing the button)
    Dim wsNext As Worksheet     'Used to store and reference the next sheet
    Dim rngFound As Range       'Used to find a matching cell in the next sheet, if any
    Dim rngText As Range        'Used to store and reference the cell that will contain the text
    Dim sFind As String         'Used to store and reference the text in wsActive, cell B2

    'Set variables
    Set wb = ActiveWorkbook
    Set wsActive = wb.ActiveSheet
    Set rngText = wsActive.Range("B2")
    sFind = wsActive.Range("B2").Value

    'Perform error checking and return appropriate errors

    'Check if text to search for was provided
    If Len(sFind) = 0 Then
        rngText.Select
        MsgBox "No text provided in cell " & rngText.Address(0, 0), , "No Search Value"
        Exit Sub
    End If

    'Check if there is a sheet after the activesheet
    If wsActive.Index = wb.Sheets.Count Then
        MsgBox "There is not a sheet after this one to search on", , "Next Sheet Unavailable"
        Exit Sub
    End If

    'Next sheet found, set the wsNext variable and search for the text
    Set wsNext = wb.Sheets(wsActive.Index + 1)
    Set rngFound = wsNext.Columns("A").Find(sFind, , , xlWhole)

    'Check if anything was found
    If rngFound Is Nothing Then
        'Nothing found, return error
        MsgBox "No matches found for [" & sFind & "] within column A of " & wsNext.Name, , "No Matches"
    Else
        'Match found, prompt if user wants to go to its location
        If MsgBox("Match found for [" & sFind & "] at '" & wsNext.Name & "'!" & rngFound.Address(0, 0) & Chr(10) & "Go to cell?", vbYesNo, "Match Found") = vbYes Then
            wsNext.Activate
            rngFound.Select
        End If
    End If

End Sub

此外,您可以使用Inputbox执行此操作,而不是使用单元格B2作为文本条目。代码大致相同,我将它放在这里供您比较/对比,以及希望学习如何做两种方法。请注意,此方法并不需要检查是否有下一张纸,因为我们不使用输入单元格。它只需要知道要搜索的工作表。

Sub btnFindText2()

    'Declare variables
    Dim wb As Workbook          'Used to store and reference the ActiveWorkbook
    Dim wsSearch As Worksheet   'Used to store and reference the worksheet that will be searched
    Dim rngFound As Range       'Used to find a matching cell in the next sheet, if any
    Dim sFind As String         'Used to get the search text from an inputbox

    'Set variables
    Set wb = ActiveWorkbook
    Set wsSearch = wb.Sheets("Sheet3")      'In your provided sample code, you searched on Sheet3. Update this to correct sheetname
    sFind = InputBox("Enter Part Number:")

    'Perform error checking and return appropriate errors

    'Check if text to search for was provided
    If Len(sFind) = 0 Then Exit Sub 'Pressed cancel

    'Because we're using an inputbox, no need to use the Next Sheet stuff
    'Just need to search for the text
    Set rngFound = wsSearch.Columns("A").Find(sFind, , , xlWhole)

    'Check if anything was found
    If rngFound Is Nothing Then
        'Nothing found, return error
        MsgBox "No matches found for [" & sFind & "] within column A of " & wsSearch.Name, , "No Matches"
    Else
        'Match found, prompt if user wants to go to its location
        If MsgBox("Match found for [" & sFind & "] at '" & wsSearch.Name & "'!" & rngFound.Address(0, 0) & Chr(10) & "Go to cell?", vbYesNo, "Match Found") = vbYes Then
            wsSearch.Activate
            rngFound.Select
        End If
    End If

End Sub

编辑:更新了Inputbox方法代码,使其不使用wsNext部分,对代码进行微调,以提高清晰度,可读性和调试性。

答案 1 :(得分:0)

只需将What作为Dim r As Range Set r = Sheets("Sheet3").Range("A:A").Find(What:=Range("B2"), _ LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False) If Not r Is Nothing Then Debug.Print "Found at " & r.Address ' If you want to activate it... r.Activate End If 参数的值。例如:

xlFormulas

如果您要搜索而不是公式(这是原始宏正在执行的操作),请将xlValues替换为TargetConditionals.h