Excel VBA UserForm Vlookup错误

时间:2015-04-07 16:05:16

标签: excel vba vlookup userform

好的,所以这是我的UserForm的代码:

Private Sub CancelButton_Click()

Unload Me

End Sub

Private Sub ClearButton_Click()

Call InventoryEntryBox_Initialize

End Sub

Private Sub SubmitButton_Click()
Dim emptyRow As Long

'Make Inventory Test sheet active
Worksheets("InventoryTest").Activate

'Transfer Information
Worksheets("InventoryTest").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = PartNumberComboBox.List
Worksheets("InventoryTest").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = LocationTextBox.Value
Worksheets("InventoryTest").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0).Value = QuantityTextBox.Value
Worksheets("InventoryTest").Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = CommentsTextBox.Value

Call InventoryEntryBox_Initialize

End Sub

Private Sub InventoryEntryBox_Initialize()

'Fill PartNumberComboBox
PartNumberComboBox.List = ActiveWorkbook.Sheets("Test2").Range("B2:B43").Value

'Empty Location Text Box
LocationTextBox.Value = ""

'Empty Quantity Text Box
QuantityTextBox.Value = ""

'Empty Comments Text Box
CommentsTextBox.Value = ""

'Unit Of Measure auto-fill box


'Description auto-fill box
DescriptionFormula.Value = Application.WorksheetFunction.VLookup(PartNumberComboBox.List, ActiveWorkbook.Sheets("Test2").Range("B2:D43"), 3, False)

'Set focus on Empty Part Number text box
PartNumberTextBox.SetFocus

End Sub

现在,使用此代码的想法是拥有用户表单,以便有人可以填写它(PartNumberComboBox,LocationTextBox,QuantityTextBox和CommentsTextBox),以便列出哪些项目在哪些地方和数量的库存。当某人填写PartNumberComboBox中的项目编号时,DescriptionFormula将自动填充列表中的数据(注释为PartDescription,其中包括表单Test2中的D2:D43)。

然而,我的问题有两方面:当有人输入PartNumberComboBox中的部件号时,当我尝试点击“提交”按钮确认时,DescriptionFormula.Value框不会自动填充数据。在表单中输入的数据并将其放在代码中指定的区域中,我弹出一个对话框并显示“运行时错误70:权限被拒绝”,然后是" PartNumberComboBox.List = ActiveWorkbook.Sheets (" Test2的")范围(" B2:B43&#34)。值及#34;我去调试时会突出显示。

我不确定我的代码是否有问题,或者基于我正在做的事情是否是Excel或Vlookup的限制......或者它是否是其他内容。任何人都能提供的帮助在这一点上都是一种祝福。

1 个答案:

答案 0 :(得分:0)

我实际上已经解决了我自己的问题:

所以事实证明我需要为这个特定的等式编写一个新子,而不是将它嵌入到我的Initialize子中。以下是我为了让它发挥作用而写的内容:

Private Sub PartNumberComboBox_Change()
Me.UnitOfMeasureFormula.Value = Application.WorksheetFunction.VLookup(Me.PartNumberComboBox.Value, Sheets("sheet2").Range("a2:c43"), 3, False)
Me.DescriptionFormula.Value = Application.WorksheetFunction.VLookup(Me.PartNumberComboBox.Value, Sheets("sheet2").Range("a2:c43"), 2, False)
End Sub

将我的“描述自动填充框”更改为:

'Empty Description label
DescriptionFormula.Value = ""

这样做会清空自动填充将在Userform启动时进入的框,然后使用带有公式引用的ComboBox的_Change子进程,以便根据ComboBox的说法改变DescriptionForumula一个简单的VLookup公式。现在它运作顺利,我又回到了正轨!