根据单元格的内容进行search

我有一个命令button设置,我给它分配了一个macros。 我需要button来获取单元格B2的内容,然后在下一张表的A列中search它。 这是我的代码。 正如你所看到的,它正在寻找当我loggingmacros的时候出现的字面文本。 我怎样才能searchB2input的内容?

 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 

这可能是你所需要的矫枉过正,但我​​一直相信错误检查,以及使用完整和灵活的代码,所以这就是你所要求的,与评论:

 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作为文本条目。 代码大部分是相同的,我把它放在这里为你比较/对比,以及希望学习如何做这两种方法。 请注意,此方法不需要检查是否有下一张纸,因为我们没有使用input单元格。 它只需要知道要search的表格。

 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 :更新了Inputbox方法代码,以便它不使用wsNext部分,为了清晰,可读性和debugging对代码进行了微小的调整。

只需传递Range("B2")作为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 

如果您想search而不是公式 (这是您的原始macros正在做的),用xlFormulasreplacexlValues