2017-09-13 125 views
0

我设法创建了一个代码,它使用visual basic在Excel数据库内搜索所有选项卡。但是,搜索只会告诉我信息所在的位置,并且不会显示信息。使用VBA在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 

我需要的是,当你运行搜索,而不是告诉我在哪里,该数据位于要显示的数据。

请任何人都可以帮我解决这个问题吗?如果您需要任何其他信息,请告诉我。 据我所知,有

+0

“找到=真”,把这段代码 - >“cell.Select – ExcelinEfendisi

+0

就这样做了,这是没有工作:( – Elliot

+0

在此之前,把这个代码以及因为我错过了单元格可能在另一个工作表 - >表格(cell.Parent.Name).Select,或在一行 - > - Application.Goto表单(cell.Parent.Name).range(cell.Address) – ExcelinEfendisi

回答

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 
+0

这实际上有效。但是,需要在两者之间进行延迟,以便让自己留在这些页面上。我们正在接近。如果可能的话,我们可以过滤掉数据 – Elliot

+0

为什么延迟?您能更准确地了解您的请求吗? – ExcelinEfendisi

+0

怎么样:在我添加的代码后,也添加这个。 ActiveCell.CurrentRegion.AutoFilter Field:= activecell.column,Criteria1:= activecell.value – ExcelinEfendisi

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 
+0

数据未显示,只是直接告诉我单词在表格上的位置。我只需要搜索,然后过滤信息以显示数据。 – Elliot

+0

你用我建议的那个替换了你的msgbox行吗?这应该告诉你:'“Word XYZ在$ TABNAME表单上的$ A $ 14中找到,单元格包含:123XYZab”' – CLR

0

如果这个词可以在多张纸上找到,这将过滤所有包含单词的表格

另外,如果用户输入野生字符,它将停止搜索CTER “*”


Option Explicit 

Public Sub SearchWordInEntireWorkbook() 
    Dim ws As Worksheet, findWhat As String, foundCell As Range, found As Boolean 

    findWhat = Application.InputBox("Enter word to search for in this workbook?", _ 
            "Search Word!", Type:=2) 
    If Len(findWhat) = 0 Or findWhat = "*" Then 
     MsgBox "Try again", vbExclamation, "Search Cancelled!" 
     Exit Sub 
    End If 

    For Each ws In ThisWorkbook.Worksheets 
     With ws.UsedRange 

      If ws.AutoFilterMode Then .AutoFilter 

      Set foundCell = .Find(What:=findWhat, _ 
            After:=.Cells(.Rows.Count, .Columns.Count), _ 
            LookAt:=xlPart, MatchCase:=False) 

      If Not foundCell Is Nothing Then 
       .AutoFilter Field:=foundCell.Column, Criteria1:=findWhat 
       found = True 
      Else 
       .Rows.Hidden = True 
      End If 
     End With 
    Next ws 

    If Not found Then 
     MsgBox "The word '" & findWhat & "' was not found in this workbook.", _ 
       vbExclamation, "Word Not Found!" 
    End If 
End Sub 
+0

只是因为某种原因尝试了这个功能,它仍然显示并告诉我信息在哪里。现在让我在空白工作表上给它一个测试,然后回复你。但是,似乎做同样的事情不通过它发现的数据进行过滤。 – Elliot

+0

现在运行时错误1004.让我再试一次masterdatabase。 – Elliot