2016-09-28 156 views
-2

我想編寫一個宏來搜索數據庫中的列,並在不同的選項卡中返回包含特定給定關鍵字的所有單元格。換句話說,我想輸入「錘子」,並獲取每個包含單詞錘子的單元格列表,即使該單詞處於值的中間(例如,「建設者昨天買了一把錘子」)。查找在excel中包含關鍵字的單元格VBA

我對VBA相當陌生,所以我想就使用哪些函數來做一些幫助/輸入。我試過使用AdvancedFilter,但是它只能看到每個單元格值的開始。歡迎所有反饋,謝謝!

對象使用方法的 Range
+3

你是指Excel中的數據庫還是表格?我很確定,如果它全部在Excel中,您不需要VBA。你只需要一些Excel公式 – AER

+0

請澄清你的答案。如果不是,你會傾向於投票,沒有人會打擾你的問題。 – AER

回答

0

Autofilter()

Sub main() 
    With Worksheets("hammer") '<--| reference searched worksheet (change "hammer" to its actual name) 
     With .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)) '<--| reference its column "A" cells from row 1 down to last non empty one (change "A"s to your actual searched column index) 
      .AutoFilter Field:=1, Criteria1:="*hammer*" '<--| filter on referenced column to get cell containing "hammer" 
      If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets("different tab").Range("A1") '<--| copy any filtered cell into "different tab" worksheet 
     End With 
     .AutoFilterMode = False '<--| show all rows back 
    End With 
End Sub 

它假設的搜索範圍中的第一小區(「A1」在本例中)是一個標頭細胞,使得它不會被搜索「 *錘子」。

相關問題