【问题标题】:Userform_Initialize not working; Error 424 or error 91Userform_Initialize 不起作用;错误 424 或错误 91
【发布时间】:2017-05-17 04:24:01
【问题描述】:

我正在使用 Excel 2016,而且我是 VBA 新手。用户表单initialize 功能正常工作,然后停止。我正在尝试找出原因。

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

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

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

我是否采取了错误的方法?

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

这是用户窗体上的代码:

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

我认为以某种方式涉及关键字或其他内容。当我更改函数的名称时,我看到窗口顶部的一些内容发生了变化。它从“用户表单”到“常规”。我认为这很重要。

编辑 2

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

【问题讨论】:

  • 您是否厌倦了使用“Private Sub UserForm_Activate()”?或在您的循环之前添加“On Error resume next”,然后在循环之后添加“On error goto 0”。我知道这不是最安全的解决方法,但它可以帮助缩小您真正的问题所在。打开弹出窗口时也可以尝试“WareHouseCheckinForm.Show False”。希望有帮助
  • 你说的我都不知道该怎么办了。
  • 我无法理解您通过重命名子来修复错误 424 的想法。 VBA 中的事件处理程序必须具有特定名称,这就是它们被理解为事件处理程序的方式。在上一个屏幕截图中,您可以在左侧下拉列表中看到 UserForm,在右侧下拉列表中看到 Initialize,这就是您知道 sub 是 事件处理程序的方式。您永远不会手动键入事件处理程序名称,您从两个下拉列表中选择事件并为您生成名称。重命名 sub 并以适当的方式修复您遇到的任何错误。
  • 将Option Explicit 作为代码模块的第一行。
  • @DanWhaley:小提示:您不应该将解决方案放入问题中。由于您已经发布了相应的答案,因此它也是多余的。

标签: vba excel


【解决方案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

【讨论】:

  • 感谢您花时间写出来。 idk 如果它看起来很苛刻,但我只是说我对 VBA 的了解不够,无法接受你所说的并用它来运行。
  • 没问题,我重读了一遍,明白你为什么这么说。只是想帮你。让我知道这是否有帮助,因为还有另一种简单的解决方案,而是编辑上面的代码而不是尝试解释。我建议也许从“UserForm_Initialize()”调用 sub。也就是说,如果“Private Sub UserForm_Activate()”不起作用。
【解决方案2】:

原来我只使用= 设置范围,而不使用set。我没有考虑过如何设置对象与原语。

我变了:

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

到:

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

现在一切都如我所愿。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多