我有Sheet2,其數據(產品名稱)按字母順序排列,產生26列,另加一列數字數據。在Sheet1上有一個近10,000個產品名稱,無論我們在Sheet2上是否有它們,都要進行測試。要一次搜索一個名字是一個熱潮,所以我一直在想,如果我們可以在Sheet1中批量搜索數據,並且每個單元格旁邊都會顯示搜索結果,並在Sheet2中找到該產品的單元格的地址,還是沒有找到,如:Excel多字符串搜索
SEARCH STRINGS SEARCH RESULT
Vodafone A4
Mirinda C105
Coca-Cola Y59
HeroHonda not found
Bournvita S27
Maggi not found
我已經做了一些搜索,找到了上面需要什麼類似的代碼,發現了以下代碼:http://www.excelforum.com/excel-programming-vba-macros/714965-search.html,這確實爲一個字符串搜索。可以修改這個腳本來獲得想要的結果嗎?
Sub DataSearch()
Dim Data() As Variant
Dim DstWks As Worksheet
Dim Food As String
Dim N As Variant
Dim R As Long
Dim Rng As Range
Dim RngEnd As Range
Dim SrcWks As Worksheet
Set SrcWks = Worksheets("Test 2")
Set DstWks = Worksheets("Test1")
R = 6
Food = DstWks.Range("E3")
N = DstWks.Range("E4")
If DstWks.Range("C6") <> "" Then
DstWks.Range("C6").CurrentRegion.Offset(0, 1).ClearContents
End If
Set Rng = SrcWks.Range("A4:E4")
Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, SrcWks.Range(Rng, RngEnd))
ReDim Data(1 To Rng.Rows.Count, 1 To Rng.Columns.Count)
Data = Rng.Value
For I = 1 To UBound(Data, 1)
If Data(I, 1) = N And InStr(1, Data(I, 3), Food, vbTextCompare) > 0 Then
DstWks.Cells(R, "C").Resize(1, Rng.Columns.Count) = Rng.Rows(I).Value
R = R + 1
End If
Next I
End Sub
總是感謝您的寶貴幫助。
terry歡迎來到StackOverflow :)檢查此[發表。](http://stackoverflow.com/questions/14578474/copy-to-various-columns-based-on-cell-contents/14584298#14584298)如上所述,通過大數據範圍進行迭代搜索可能導致巨大的性能下降。您可以合併*通配符*方法並嘗試。我在這裏支持數組。 – bonCodigo 2013-04-11 09:17:24