2013-08-01 38 views
0

所以我得到了兩個工作簿。 第一個包含源信息。 第二個包含應使用來源信息中的信息完成的產品列表。根據條件從工作簿中提取值

所以我認爲這可以用VBA來完成。這是我的主意:

選擇從塔B的標準值 搜索在源工作表的標準 選擇行,其中的匹配標準是(例如4行) 選擇柱Q和W的細胞在匹配的行中,將這些值複製回標準所在行中產品工作簿的單元格E和F中。

這個可以在VBA中實現嗎?你有什麼提示可以幫助我嗎?先謝謝你!

+3

你可以用'VLOOKUP' – Ripster

+0

非常感謝你這樣做! – chrissik

回答

1

如果兩個工作簿中的條件單元格嚴格相同,我建議使用源表格的條件創建一個數組,然後遍歷產品表以在數組中添加所需的2列。 您只需要通過源表單再次循環,並用相關數據替換目標單元格。 如果排序順序沒有重要性,或者可以重置,我會建議在您的標準列上進行排序,以便使用單個For ... Next循環進行優化。

如果您的標準單元格不完全相同,是否有可以重複使用的模式?

一個簡單的代碼示例是這樣:

Sub CopyData() 
Dim myData(200, 3) As Variant 
Dim i As Integer 
Dim iArrayLimit As Integer 
Dim myLastRow As Integer 

Application.Workbooks(Source).Activate 
myLastRow = ActiveCell.SpecialCells(xlCellTypeLastCell).Row 
iArrayLimit = myLastRow 
For i = 1 To myLastRow 'Provided your source sheet has no header. Replace by 2 if it does! 
myData(i, 1) = Cells(i, 2) 'Column B, right 
Next 

Application.Workbooks(Products).Activate 
myLastRow = ActiveCell.SpecialCells(xlCellTypeLastCell).Row 
For i = 1 To iArrayLimit 'Take care: if you have headers here and not in the source, change the code below 
For j = 1 To myLastRow 
    If Cells(j, 1) = myData(i, 1) Then 'If your criteria in the products sheet is in column A! 
    myData(i, 2) = Cells(j, 17) 
    myData(i, 3) = Cells(j, 23) 
    Exit For 
    End If 
Next 
Next 

Applications.Workbooks(Source).Activate 
For i = 1 to iArrayLimit 
Cells(i, 5) = myData(i, 2) 
Cells(i, 6) = myData(i, 3) 
Next 

End Sub 
+0

非常感謝 – chrissik

相關問題