首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用索引匹配(VBA)更新数据库

用索引匹配(VBA)更新数据库
EN

Stack Overflow用户
提问于 2016-04-26 06:55:12
回答 1查看 184关注 0票数 0

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

图片:过滤表数据库的一小部分

我使用索引匹配函数。只有在产品代码和交付编号匹配(唯一)的情况下,通过使用筛选表的剩余数量更改DB的数量来更新匹配的行。

代码语言:javascript
复制
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函数的其他部分缺少了什么。如果找不到匹配,它只会跳到下一个传递号,并忽略行。但它应该搜索直到它被找到。

对这个案子有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-26 12:38:35

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

范围是:

代码语言:javascript
复制
table6 = Sheet1.Range("G2:G" & lastrowG)

阿卜杜勒

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36857405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档