【问题标题】:Updating Database with Index-Match (VBA)使用索引匹配 (VBA) 更新数据库
【发布时间】:2016-04-26 06:55:12
【问题描述】:

我有一个重要的问题。我有一个小数据表(过滤表)和我的大数据库(大约 23'000 行)。结构是一样的。我想用过滤表的剩余数量(sheet1 中的第 5 列)更新数据库 qty(sheet2 中的第 4 列),以查看库存的当前状态。

图片: 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 部分缺少某些内容。如果找不到匹配项,它只会跳转到下一个交货编号并忽略该行。但它应该搜索直到找到。

请对这个案例有什么建议吗?

【问题讨论】:

  • @Om3r,你怎么看?有什么好的解决办法吗?

标签: vba excel for-loop indexing match


【解决方案1】:

问题解决了。我只是改变了for循环的范围。这是一个逻辑错误。

范围是:

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

阿卜杜勒

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2016-11-30
    • 2015-12-15
    • 2021-05-25
    • 2021-06-14
    • 2021-08-25
    相关资源
    最近更新 更多