Userform_Initialize不工作; 错误424或错误91

我正在使用Excel 2016,而我是VBA的新手。 用户表单initializefunction正在工作,然后停止。 我想弄清楚为什么。

我想在工作表上按下一个button,popup一个窗体,接受一些input(文本和从下拉列表中select),另一个button在窗体上创build另一个popup窗口接受更多的input(条形码扫描或文本条目),直到它最终根据确定的条件退出(用尽槽以填充扫描的条形码)或者用户退出。

我有工作表上的button。 我有用户表单。 但是,在某些时候,我不得不将userform_Initialize重命名为<formName>_Initialize因为我得到了有关丢失对象的错误,并且出现错误424.在这样做之后,“编译器”很高兴,但是initialize函数从不被调用,正在处理用户表单。

我认为我所看到的与其他问题非常相似,但我不确定,如果我尝试这样做,我也不确定在哪里粘贴我的代码。

我采取了错误的做法?

Private Sub UserForm_Initialize()在运行时给出错误424,当我点击commandButton调出Private Sub UserForm_Initialize() 。 切换到Private Sub warehouseCheckinForm_Initialize() ,当用户窗体填充时,永远不会调用初始化函数,从而导致窗体上的function被破坏并导致其他问题。

这是用户表单上的代码:

 Public initialsInput As String '*Initials of user scanning in stuff Public modelSelected As String '*Model selected for scanning Public numItemsModel As Integer '*Keep track of how many of the selected model are available to check in to the warehouse Public searchBOMRange As Range '*Range for search operations Private Sub ModelComboBox_Change() modelSelected = ModelComboBox.Value numItemsModel = Application.WorksheetFunction.CountIf(searchBOMRange, modelSelected) numItemsModelLabel.Caption = numItemsModel End Sub Private Sub setInitialsButton_Click() If Len(InitialsTextBox.Value) = 2 Then initialsInput = InitialsTextBox.Value ElseIf Len(InitialsTextBox.Value) < 2 Then MsgBox "Enter in 2 letters for your initials" Else MsgBox "You entered in too much data!" End If End Sub Private Sub UserForm_Initialize() '*Start with empty inputs numItemsModel = 0 searchBOMRange = Sheets("BOM").Range("C11:C2000") modelSelected = "" initialsInput = "" InitialsTextBox.Value = "" ModelComboBox.Clear numItemsModelLabel.Caption = numItemsModel '*Fill the Combo Boxes Dim oDictionary As Object Dim cellContentModel As String Dim rngComboValues As Range Dim rngCell As Range Set rngComboValues = Sheets("BOM").Range("C11:C2000") '*The first ~2000 items because there probably won't be BOMs bigger than that. Test case was <1000 '*Doing C:C took 5+ seconds to load the window Set oDictionary = CreateObject("Scripting.Dictionary") For Each rngCell In rngComboValues cellContentModel = rngCell.Value If Not oDictionary.exists(cellContentModel) Then oDictionary.Add cellContentModel, 0 End If Next rngCell For Each itm In oDictionary.keys Me.ModelComboBox.AddItem itm Next itm Set oDictionary = Nothing 'For Each cell In Sheets("BOM").Range("B:B") ' If cell.Value <> "" Then ' MakeComboBox.AddItem cell.Value ' End If 'Next cell End Sub Private Sub warehouseScanButton_Click() For Each modelSelected In searchBOMRange If Len(initialsInput) < 2 Then Beep MsgBox "Enter your initials first!" End ElseIf Len(modelSelected) < 1 Then Beep MsgBox "Select a model first!" End ElseIf Len(initialsInput) >= 2 And Len(modelSelected) >= 1 Then scannedInput = InputBox("Scan a serial number, or type it in and mash the ENTER key") If scannedInput = "NA" Or scannedInput = "N/A" Then Beep MsgBox "You can't search for 'not applicable', it doesn't apply!" End End If End If '//Searches for empty serial number cell '// Model is in C, serial is in O (letter) '//offset is row, column; down is positive, right is positive Set matchedCell = modelSelected.Offset(0, 12) If matchedCell Is Nothing Then '//do stuff scannedInput = InputBox("Scan a serial number, or type it in and mash the ENTER key") matchedCell.Offset(0, 2).Value = initialsInput matchedCell.Offset(0, 3).Value = Now '// Checked in to Warehouse matchedCell.Offset(0, -2).Value = Now '// "Recv'd date" matchedCell.Offset(0, 1).Value = "W" numItemsModel = numItemsModel - 1 'If Len(matchedCell.Offset(0, 4).Value) >= 2 And scannedInput <> "" Then ' Beep ' MsgBox "Serial Number " & scannedInput & " is already checked in to The Lab!" 'ElseIf Len(matchedCell.Offset(0, 4).Value) < 2 Then ' matchedCell.Offset(0, 4).Value = initialsInput ' matchedCell.Offset(0, 5).Value = Now ' matchedCell.Offset(0, 1).Value = "L" End If If Not matchedCell Is Nothing Then '//If the cell has something in it '//Beep '//MsgBox "Error! This is unpossible!" '//End End If End If Next modelSelected End Sub 

这是工作表上命令button的逻辑:

 Private Sub WarehouseCheckinCommandButton_Click() '*Brings up the form WareHouseCheckinForm.Show End Sub 

我想不知何故涉及到一个关键字,或其他的东西。 当我改变函数的名字时,我看到窗口顶部的一些东西在改变。 它从“Userform”到“General”。 我认为这很重要。

在这里输入图像描述

在这里输入图像描述

编辑2

(编辑1是在狡猾的滚动)好吧,所以这听起来像我需要离开初始化函数Userform_Initialize。 这是当我点击命令buttonrun time error 91 object variable or With block variable not set ,我可以selectdebugging。 如果我debugging,我得到这个:

在这里输入图像描述

对不起,不好解释自己。 这似乎是你试图做很多“私人小组UserForm_Initialize()”。 我build议也许把它分开。 例如下面。 希望有所帮助。 我添加了有关使用error handling程序的注释,以帮助您找出哪个循环从错误中获取错误。

 Private Sub UserForm_Initialize() '*Start with empty inputs numItemsModel = 0 searchBOMRange = Sheets("BOM").Range("C11:C2000") modelSelected = "" initialsInput = "" InitialsTextBox.Value = "" ModelComboBox.Clear numItemsModelLabel.Caption = numItemsModel '*Fill the Combo Boxes End Sub Private Sub UserForm_Activate() Dim oDictionary As Object Dim cellContentModel As String Dim rngComboValues As Range Dim rngCell As Range Set rngComboValues = Sheets("BOM").Range("C11:C2000") '*The first ~2000 items because there probably won't be BOMs bigger than that. Test case was <1000 '*Doing C:C took 5+ seconds to load the window Set oDictionary = CreateObject("Scripting.Dictionary") 'On error resume next ' Turn these on to single out with loop is giving the error For Each rngCell In rngComboValues cellContentModel = rngCell.Value If Not oDictionary.exists(cellContentModel) Then oDictionary.Add cellContentModel, 0 End If Next rngCell 'On error goto 0 ' this resets the your error 'On error resume next ' Turn these on to single out with loop is giving the error For Each itm In oDictionary.keys Me.ModelComboBox.AddItem itm Next itm 'On error goto 0 ' this resets the your error Set oDictionary = Nothing 'For Each cell In Sheets("BOM").Range("B:B") ' If cell.Value <> "" Then ' MakeComboBox.AddItem cell.Value ' End If 'Next cell End Sub 

事实certificate,我正在设置范围只是=而不是使用set 。 我没有想到如何设置一个对象与一个基元。

我变了:

 searchBOMRange = Sheets("BOM").Range("C11:C2000") 

至:

 Set searchBOMRange = Sheets("BOM").Range("C11:C2000") 

现在事情就像我想要的那样工作。