Userform_Initialize不起作用;错误424或错误91

时间:2017-03-14 18:11:08

标签: excel vba excel-vba

我正在使用Excel 2016,我是VBA的新手。 userform initialize功能正在运行,然后停止。我想弄明白为什么。

我想在工作表上按一个按钮,弹出一个表单接受一些输入(文本和下拉列表中的选项),然后在表单上另一个按钮创建另一个弹出窗口以接受更多输入(条形码扫描或文本输入),直到它最终根据确定的条件退出(用完填充扫描的条形码的插槽)或用户退出。

我在工作表上有按钮。我有用户表单。但是,在某些时候,我不得不将userform_Initialize重命名为<formName>_Initialize,因为我收到有关对象丢失的错误,并且错误424.这样做之后,“编译器”很高兴,但初始化函数是从未调用过,所以没有任何东西在用户表单上工作。

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

我采取了错误的做法吗?

当我单击commandButton以显示userform时,

Private Sub UserForm_Initialize()在运行时给出错误424。切换到Private Sub warehouseCheckinForm_Initialize()时,初始化函数永远不会在用户窗体填充时调用,导致窗体上的功能损坏并导致其他问题。

这是userform上的代码:

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

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

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

End Sub

我认为某种关键词涉及或其他方面。当我更改函数的名称时,我看到窗口顶部有些东西在变化。它从“Userform”变为“General”。我认为这很重要。

enter image description here

enter image description here

修改2

(编辑1在狡猾地滚动)好的,所以听起来我需要将初始化函数保留为Userform_Initialize。这是我点击命令按钮run time error 91 object variable or With block variable not set时得到的,我可以选择调试。如果我调试,我得到这个:

enter image description here

2 个答案:

答案 0 :(得分:1)

很抱歉给自己解释不好。看起来你试图用“Private Sub UserForm_Initialize()”做很多事情。我建议可能把它分开。例如下面。希望有所帮助。我添加了关于使用错误处理程序的注释,以帮助找出从中获取错误的循环。

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

答案 1 :(得分:0)

事实证明,我只使用=而不使用set来设置范围。我没有想过如何设置一个对象与一个原语。

我改变了:

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

为:

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

现在事情就像我希望他们一样。