2016-04-26 3 views
0

私は重要な質問があります。私は小さなデータテーブル(フィルタリングされたテーブル)と私の大きなデータベース(約23000行)を持っています。構造は同じです。私はデータベースの量(シート2の第4列)を残りの数量(シート1の第5列)で更新して、ストックの現在の状態を確認したいと考えています。インデックスマッチ(VBA)でデータベースを更新する

画像: Filtered TableSmall part of the Database

私は、インデックス・マッチ機能を使用しています。製品コードと出荷番号が一致する場合(ユニーク)にのみ、DBの数量をフィルタリングされたテーブルの残量で変更して、一致する行を更新します。

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

、あなたはどう思いますか?そのための良い解決策はありますか? – Abduuul

答えて

0

問題は解決しました。 forループの範囲を変更しました。それは論理的な誤りでした。

範囲は次のとおりです。Om3r @

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

アブドゥル

関連する問題