简体   繁体   English

如何将Vlookup单元格设置为VBA中变量表示的值?

[英]How do I set a vlookup cell to a value represented by a variable in VBA?

I have a list of incoming inventory with part numbers and corresponding incoming amounts in one sheet and a comprehensive part list with current inventory amounts in another. 我在一张纸上有一份备有零件编号和相应进货金额的进货清单,在另一张纸上有一份详细的清单,其中有当前库存金额。 I want to be able to run code that adds the incoming inventory amounts to the current inventory. 我希望能够运行将传入的库存金额添加到当前库存的代码。 So far, I am able to get the code to assign the new inventory value to a variable cur_inv but I cannot get it to push that new value to the correct cell. 到目前为止,我已经能够获得将新库存值分配给变量cur_inv的代码,但是我无法获取将新值推入正确单元格的代码。 Here's the code: 这是代码:

Sub Test()
ActiveSheet.Range("A1").Select
Do Until IsEmpty(ActiveCell)

    lookfor = ActiveCell.Offset(0, 1).Value
    Set r = Sheets("Available Inventory").Range("Lookup")
    cur_inv = Application.WorksheetFunction.VLookup(lookfor, r, 2, False)
    new_inv = ActiveCell.Offset(0, 2).Value

    cur_inv = new_inv + cur_inv

    ActiveCell.Offset(1, 0).Select
Loop

End Sub

您可以通过将变量的值设置为单元格的值来做到这一点,例如:

ActiveSheet.Range("Your Cell here").Value = new_inv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM