2017-09-13 155 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