簡體   English   中英

Excel VBA UserForm Vlookup錯誤

[英]Excel VBA UserForm Vlookup error

好的,這是我的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”))。調試時突出顯示Range(“ B2:B43”)。Value“。

我不確定這是我的代碼是否有問題,還是基於我在做的事是否是Excel或Vlookup的局限性……還是其他原因。 在這一點上,任何人都可以提供的任何幫助都是一種祝福。

我實際上已經解決了自己的問題:

因此,事實證明我需要為此特定方程式編寫一個新的子程序,而不是將其嵌入到我的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 = ""

這樣做是在用戶窗體啟動時清空自動填充框,然后將_Change子與公式所引用的ComboBox一起使用,以便根據ComboBox的內容通過簡單的VLookup更改DescriptionForumula式。 現在,它運行順利,我又回到了正軌!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM