2016-03-09 49 views
-1

我試圖創建一個宏來搜索名爲DB的工作表中的單元格B5的值,並將所有結果粘貼到名爲Research的工作表中。我們的想法是複製與關鍵字匹配的每一行,並將它們從數據庫表中的B11開始粘貼。在Excel中查找,複製和粘貼搜索引擎

我不知道是否有可能,但提前感謝您的時間。

+1

是的,它是可能的。開始使用一些代碼([宏記錄器](https://support.office.com/zh-cn/article/Step-1-Start-with-the-macro-recorder-6DC53056-1DE1-4483-AA07- 63E4E0EFE3C2)應該是一個很好的開始),如果你有問題,請隨同你的警察回來。 – Jeeped

回答

0

子CreateList()

Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 

Dim LastRow As Long 
Dim I As Integer 
Dim J As Integer 
Dim srchtxt As String 
Dim celltxt As String 



'Determines last row of database worksheet to know what range to loop through 
LastRow = Worksheets("DB").Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 

'Gets the text or value to be searched 
srchtxt = Worksheets("SearchWS").Range("B5") '***Change cell to search 

'Clear research sheet, the destination sheet 
Worksheets("Research").Cells.Clear 

'Activate DataBase sheet 
Worksheets("DB").Activate 

'Loops through and copies all rows with desired value or text, pasting them in the research sheet. j keeps track of the next empty row. 
'The InStr and UCase ensure capitalization doesn't cause a problem. You may not want this if you need exact match. 
J = 2 '*** Change the first row to paste 
For I = 2 To LastRow 
    celltxt = Worksheets("DB").Cells(I, 1).Text 'Gets the value from the DB worksheet ***Change the column to seach in 
    If InStr(1, UCase(celltxt), UCase(srchtxt)) Then 'Compares it to the specified text, B5 in this case 
    Worksheets("DB").Range(Cells(I, 1), Cells(I, 2)).Copy Destination:=Worksheets("Research").Cells(J, 1) 
    'Copies the range above. ***Change the range to copy, from 1 to 2 
    J = J + 1 
End If 
Next I 'Loops through 

Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 

結束子

+0

你好Jcarroll,非常感謝答案,我不知道代碼是如何工作的。你如何啓動德宏,並在哪個單元格索引你正在搜索的單詞?再次感謝! –

+0

@SalimChorfi請看我更新的答案,現在應該會更有用。但是,如果您有更多問題,請告訴我。 – jcarroll