为什么这个Vlookup不工作? VBA

时间:2016-02-23 02:29:34

标签: vba excel-vba excel

我要做的是能够双击列表框中的型号,然后使用Hlookup在单独的位置查找实际列表中的值。最终会进行一个简单的计算,但我仍然坚持这个小问题。

仅为了一些额外的信息,Pricelist是包含模型#和价格的表的名称,而ListModel是列表框的名称。

Private Sub ListModel_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

Dim perDiscountRate As Double
Dim PerDiscountPrice As Currency
Dim PriceList As String

perDiscountRate = InputBox("What is the discount rate", "Discount Calculator", "10") / 100
Application.Workbooks("T6-EX-E1D.xlsm").Worksheets("Computers").Range("c6:c8").Value = perDiscountRate

perDiscountRate = Application.WorksheetFunction.VLookup("ListModel.Selected",   PriceList, 2)

End Sub

2 个答案:

答案 0 :(得分:0)

如果我猜你的目标正确,你会使用

Dim sht As Worksheet

Set sht = Application.Workbooks("T6-EX-E1D.xlsm").Worksheets("Computers")
With Me.ListModel
    PerDiscountPrice = Application.WorksheetFunction.VLookup(.List(.ListIndex), sht.Range("PriceList"), 2, 0)
End With

我认为" PriceList"被命名的范围是' in" Computers" " T6-EX-E1D.xlsm"的工作表工作簿。

如果没有,只需根据需要更改参考

答案 1 :(得分:0)

感谢您的帮助。我能够让代码工作。这就是它的样子。

Private Sub ListModel_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

Dim perDiscountRate As Double
Dim PerDiscountPrice As Currency
Dim PriceList As Range
Dim Org As Worksheet

Set Org = Application.Workbooks("T6-EX-E1D.xlsm").Worksheets("Computers")


perDiscountRate = InputBox("What is the discount rate", "Discount Calculator", "10") / 100
Application.Workbooks("T6-EX- E1D.xlsm").Worksheets("Computers").Range("c6:c8").Value = perDiscountRate
PerDiscountPrice = Application.WorksheetFunction.VLookup(ListModel,  Range("PriceList"), 2)

Application.Workbooks("T6-EX-E1D.xlsm").Worksheets("Computers").Range("d6:d8").Value = PerDiscountPrice



End Sub

我通过放置ListModel.selected使Vlookup中的第一个参数复杂化。只使用名称ListModel就足够了。

最佳, 扎克