使用VBA在Excel上进行高级搜索和过滤

时间:2017-09-13 09:53:12

标签: excel vba excel-vba

我设法创建了一个代码,使用visual basic搜索Excel数据库中的所有选项卡。但是,搜索只会告诉我信息的位置,并且不会显示信息。

以下代码如下:

Sub SearchWordInEntireWorkbook()
Dim ws As Worksheet
Dim strWhat As String
Dim cell As Range
Dim Found As Boolean
strWhat = Application.InputBox("What word do you want to search in this workbook?", "Search Word!", Type:=2)
If strWhat = "" Then
   MsgBox "No search word entered.", vbExclamation, "Search Cancelled!"
   Exit Sub
End If

For Each ws In Worksheets
   With ws.Cells
      Set cell = .Find(what:=strWhat, lookat:=xlPart, MatchCase:=False)
      If Not cell Is Nothing Then
         Found = True
         MsgBox "The Word " & strWhat & " is found in " & cell.Address(0, 0) & " on " & ws.Name & " sheet."
      End If
   End With
Next ws
If Not Found Then
   MsgBox "The word " & strWhat & " is not found on any sheet in this workbook.", vbExclamation, "Word Not Found!"
End If
End Sub

我需要的是运行搜索时显示的数据,而不是告诉我数据的位置。

任何人都可以帮我解决这个问题吗?如果您需要任何其他信息,请告诉我们。 我明白那里

2 个答案:

答案 0 :(得分:0)

目前,您使用

报告地址
 MsgBox "The Word " & strWhat & " is found in " & cell.Address(0, 0) & " on " & ws.Name & " sheet."

如果要添加存储在单元格中的值,只需添加一些内容即可报告值:

MsgBox "The Word " & strWhat & " is found in " & cell.Address(0, 0) & " on " & ws.Name & " sheet. The cell contains:" & Cell.Value

答案 1 :(得分:0)

作为我评论的延续

    Sub SearchWordInEntireWorkbook()
    Dim ws As Worksheet
    Dim strWhat As String
    Dim cell As Range
    Dim Found As Boolean
    strWhat = Application.InputBox("What word do you want to search in this workbook?", "Search Word!", Type:=2)
    If strWhat = "" Then
       MsgBox "No search word entered.", vbExclamation, "Search Cancelled!"
       Exit Sub
    End If

    For Each ws In Worksheets
       With ws.Cells
          Set cell = .Find(what:=strWhat, lookat:=xlPart, MatchCase:=False)
          If Not cell Is Nothing Then
             Found = True
             'MsgBox "The Word " & strWhat & " is found in " & cell.Address(0, 0) & " on " & ws.Name & " sheet."
             Application.Goto Sheets(cell.Parent.Name).range(cell.Address)
'edit starts
         ans = MsgBox("is this the one you r looking for. if so, say yes and quit, else say no and go to the next result", vbYesNo)
         If ans = vbYes Then
            Exit For
         End If
'edit ends
          End If
       End With
    Next ws
    If Not Found Then
       MsgBox "The word " & strWhat & " is not found on any sheet in this workbook.", vbExclamation, "Word Not Found!"
    End If
    End Sub