2

I am working with Excel 2016, and I am new to VBA. The userform initialize functionality was working, and then stopped. I am trying to figure out why.

I want to push a button on a worksheet, have a form pop up that accepts some input (text and a selection from a drop down list), and another button on the form to create another popup to accept more input (barcode scan or text entry) until it eventually exits based on a determined condition (run out of slots to populate with the scanned barcode) or the user exits.

I have the button on the worksheet. I have the userform. However, at some point, I had to rename userform_Initialize to <formName>_Initialize because I was getting errors about objects missing, and error 424. After doing so, the "compiler" is happy, but the initialize function is never called, so nothing is working on the userform.

I think what I am seeing is very similar to this other question, but I'm not sure, nor am I sure where to stick my code if I try to do that.

Am I taking the wrong approach?

Private Sub UserForm_Initialize() gives error 424 at runtime when I click on the commandButton to bring up the userform. Switching to Private Sub warehouseCheckinForm_Initialize(), the initialize function is never called when the userform populates, leaving the functionality on the form broken and causing other problems..

This is the code on the 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

This is the logic on the command button on the worksheet:

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

End Sub

I think somehow a keyword is involved, or something else. When I change the name of the function, I see some stuff at the top of the window changing. It goes from "Userform" to "General". I think that is important.

enter image description here

enter image description here

Edit 2

(Edit 1 was rolled in on the sly) Ok, so it sounds like I need to leave the initialize function as Userform_Initialize. This is what I get when I click on the command button run time error 91 object variable or With block variable not set and I have the option to debug. If I debug, I get this:

enter image description here

YetAnotherRandomUser
  • 1,320
  • 3
  • 13
  • 31
  • Have you tired using "Private Sub UserForm_Activate()"? or before your loops add "On Error resume next" and after loop add "On error goto 0". I know its not the safest workaround but it can help narrow down were your real issue lies. Also may try "WareHouseCheckinForm.Show False" when opening the popup. Hope that helps – Ricky Mar 14 '17 at 18:26
  • I don't even know what to do with what you said. – YetAnotherRandomUser Mar 14 '17 at 18:27
  • 1
    I cannot get my head around your idea of fixing an error 424 by renaming a sub. Events handlers in VBA must have certain names, which is how they are understood as event handlers. On your last screenshot you can see `UserForm` in the left dropdown and `Initialize` in the right dropdown which is how you know the sub *is* an event handler. You never type event handler names manually, you select the event from the two dropdowns and the name is generated for you. Rename the sub back and fix the whatever error you have in a proper way. – GSerg Mar 14 '17 at 18:31
  • 2
    Put `Option Explicit` as the first line of your code module. – GSerg Mar 14 '17 at 18:41
  • @DanWhaley: Small hint: You shouldn't put a solution into a question. As you have already posted a corresponding answer, it is also redundant. – honk May 16 '17 at 20:19
  • @DanWhaley: Also, you don't need to provide "edit" sections. We have the revision history for tracking edits. Instead rework you question to only present the latest state. You can modify your question as long as you don't render existing answers wrong. – honk May 16 '17 at 20:28

2 Answers2

1

Sorry for poorly explaining myself. It seems like your trying to do a lot with "Private Sub UserForm_Initialize()". I was suggesting to maybe split it up. e.g. below. Hope that helps. I added notes on using error handlers to help single out which loop your getting the error from.

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
Ricky
  • 98
  • 4
  • 1
    Thanks for taking the time to write that out. idk if it came across as harsh, but I just meant i didn't know enough about VBA to take what you said and run with it. – YetAnotherRandomUser Mar 14 '17 at 18:48
  • Not a problem i reread it and understand why you said what you said. Just wanted to help you out. let me know if this helps because there is one other easy solution but rather edit above code than try to explain. I would suggest maybe calling the sub from "UserForm_Initialize()". That is if "Private Sub UserForm_Activate()" does not work. – Ricky Mar 14 '17 at 18:56
0

It turned out that I was setting the range with just = and not using set. I didn't think anything about how to set an object versus a primitive.

I changed:

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

to:

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

And now things work like I want them to.

honk
  • 9,137
  • 11
  • 75
  • 83
YetAnotherRandomUser
  • 1,320
  • 3
  • 13
  • 31