2016-04-26 67 views
0

我有一個重要的問題。我有一個小數據表(過濾表)和我的大數據庫(大約23000行)。結構是相同的。我想用已過濾表格的剩餘數量(工作表1中的第5列)更新數據庫數量(工作表2中的第4列)以查看庫存的當前狀態。使用索引匹配更新數據庫(VBA)

圖片: Filtered TableSmall part of the Database

我用的是光學匹配功能。只有在產品代碼和交貨號匹配(唯一)的情況下,纔可以通過更改數據庫的數量和過濾表的剩餘數量來更新匹配的行。

Sub UpdateDB() 

On Error Resume Next 
Dim Dept_Row As Long 
Dim Dept_Clm As Long 

lastrow = ThisWorkbook.Worksheets(Worksheets.Count).Cells(ThisWorkbook.Worksheets(Worksheets.Count).Rows.Count, "C").End(xlUp).Row 
lastrowA = ThisWorkbook.Worksheets(Worksheets.Count).Cells(ThisWorkbook.Worksheets(Worksheets.Count).Rows.Count, "A").End(xlUp).Row 
lastrowI = Sheet1.Cells(Sheet1.Rows.Count, "I").End(xlUp).Row 
lastrowG = Sheet1.Cells(Sheet1.Rows.Count, "G").End(xlUp).Row 

table1 = ThisWorkbook.Worksheets(Worksheets.Count).Range("C2:C" & lastrow) 
table2 = ThisWorkbook.Worksheets(Worksheets.Count).Range("A2:D" & lastrowA) 
table3 = Sheet1.Range("I2:I" & lastrowI) 
table4 = Sheet1.Range("G2:K" & lastrowG) 

Dept_Clm = ThisWorkbook.Worksheets(Worksheets.Count).Range("B2").Column 
Dept_Row = ThisWorkbook.Worksheets(Worksheets.Count).Range("B2").Row 


'Checking for delivery numbers of the filtered table and the new sheet and updating the corresponding remaining quantity 
For Each dl In table3 
DLfiltered = Application.WorksheetFunction.Index(table4, Application.WorksheetFunction.Match(dl, table3, 0), 3) 
DLnewdb = Application.WorksheetFunction.Index(table2, Application.WorksheetFunction.Match(dl, table1, 0), 3) 

pcfiltered = Application.WorksheetFunction.Index(table4, Application.WorksheetFunction.Match(dl, table3, 0), 1) 
pcnewdb = Application.WorksheetFunction.Index(table2, Application.WorksheetFunction.Match(dl, table1, 0), 1) 

remainqty = Application.WorksheetFunction.Index(table4, Application.WorksheetFunction.Match(dl, table3, 0), 5) 
Row = Application.WorksheetFunction.Match(dl, table1, 0) 


'If the delivery numbers and the product codes are the same, then update quantity 
If dlnewdb = dlfiltered And pcfiltered = pcnewdb Then 
    ThisWorkbook.Worksheets(Worksheets.Count).Cells(Dept_Row, Dept_Clm) = remainqty 
End If 

Next CDN 

End Sub 

我知道在IF函數的ELSE部分缺少某些東西。如果找不到匹配項,它只會跳到下一個交貨號並忽略該行。但它應該搜索,直到找到它。

有關這種情況的任何建議嗎?

+0

@ Om3r,你有什麼感想?有沒有什麼好的解決方案? – Abduuul

回答

0

問題解決了。我只是改變了for循環的範圍。這是一個邏輯錯誤。

範圍是:

table6 = Sheet1.Range("G2:G" & lastrowG) 

阿卜杜勒