我已經撰寫了一些代碼以在打開作業簿時運行,并且它可以在我的個人計算機上完美運行。但是,將檔案發送到我的筆記本電腦后,我收到“應用程式定義或物件定義錯誤”訊息。我真的不明白為什么會發生這種情況或我如何解決這個問題。
Private Sub Workbook_Open()
'loads the combobox when book opened
MenuGenerator.miscComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuMiscellaneous").RefersToRange)
MenuGenerator.soupCombobox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuSoups").RefersToRange)
MenuGenerator.saladComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuSalads").RefersToRange)
MenuGenerator.meatComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuMeat").RefersToRange)
MenuGenerator.fishComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuFish").RefersToRange)
MenuGenerator.starchComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuStarch").RefersToRange)
MenuGenerator.veggieComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuVegetable").RefersToRange)
MenuGenerator.dessertComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("MenuDessert").RefersToRange)
End Sub
代碼設定為將我創建的命名范圍加載到組合框中。
uj5u.com熱心網友回復:
將診斷訊息框添加到代碼中。
Option Explicit
Sub Workbook_Open()
Dim wb As Workbook, ws As Worksheet, i As Integer
Dim fn, arObj, arMenu, rng As Range, obj
Dim msg As String
Set fn = Application.WorksheetFunction
Set wb = ThisWorkbook
Set ws = wb.Sheets("MenuGenerator")
arObj = Array("misc", "soup", "salad", "meat", "fish", "starch", "veggie", "dessert")
arMenu = Array("Miscellaneous", "Soups", "Salads", "Meat", _
"Fish", "Starch", "Vegetable", "Dessert")
On Error Resume Next
For i = 0 To UBound(arObj)
Set rng = Nothing
Set obj = Nothing
msg = ""
Set rng = wb.Names("Menu" & arMenu(i)).RefersToRange
If rng Is Nothing Then
msg = msg & vbLf & "Error with name range 'Menu" & arMenu(i) & "'"
End If
Set obj = ws.OLEObjects(arObj(i) & "ComboBox")
If obj Is Nothing Then
msg = msg & vbLf & "Error with '" & arObj(i) & "ComboBox" & "'"
End If
If msg = "" Then
obj.Object.List = fn.Transpose(rng)
Else
MsgBox msg, vbExclamation
End If
Next
On Error GoTo 0
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346057.html
