+ Reply to Thread
Results 1 to 2 of 2

Error 424 Object Required Error when Using AddItem to ListBox Generated in Code

Hybrid View

jclark419 Error 424 Object Required... 03-01-2010, 12:20 PM
Andy Pope Re: Error 424 Object Required... 03-01-2010, 12:47 PM
  1. #1
    Registered User
    Join Date
    07-20-2005
    Posts
    13

    Error 424 Object Required Error when Using AddItem to ListBox Generated in Code

    Hey all, I am having an error message when I ask Excel to AddItem to a listbox that was created earlier in the code. It is giving me an error 424 object required message and I can't seem to figure out why it is not referencing the listboxes I had already made. I am including the code:

    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 2/24/2010 by a0x01472
    '
    ' Keyboard Shortcut: Ctrl+e
    '
    
    ' Form development section ---------------------------------------
    
    Dim TempForm As Object
    Dim NewButton As MSForms.CommandButton
    Dim NewLabel As MSForms.Label
    Dim NewListBox As MSForms.ListBox
    Dim NewCheckBox As MSForms.CheckBox
    
    sizer = (Round(43 ^ (1 / 2), 0))
    
    'This is to stop screen flashing while creating form
    Application.VBE.MainWindow.Visible = False
    
    Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    
        'Create the User Form
    With TempForm
        .Properties("Caption") = "My User Form"
        .Properties("Width") = 10 + (sizer * 120)
        .Properties("Height") = 72 + (sizer * 54)
    End With
    
    ' End form development section --------------------------------------
    
    
    ' Data Analysis Section -------------------------------------------
    
    Dim myval As Long
    Dim relA() As String
    Dim relB() As String
    k = 1
    y = 1
    z = 1
    
    For i = 2 To 44
        namer = i - 1
        Set NewLabel = TempForm.designer.Controls.Add("Forms.label.1")
        With NewLabel
            .Name = "FieldLabel" & namer
            .Caption = Cells(34, i).Value
            .Top = (6 * y) + (48 * (y - 1))
            .Left = (6 * z) + (114 * (z - 1))
            .Width = 114
            .Height = 12
            .Font.Size = 10
            .Font.Name = "Tahoma"
            .BackColor = &H80FFFF
            .TextAlign = fmTextAlignCenter
        End With
        
        Set NewListBox = TempForm.designer.Controls.Add("Forms.listbox.1")
        With NewListBox
            .Name = "MyListBox" & namer
            .Top = (18 * y) + (36 * (y - 1))
            .Left = (6 * z) + (114 * (z - 1))
            .Width = 114
            .Height = 36
            .Font.Size = 10
            .Font.Name = "Tahoma"
            .BorderStyle = fmBorderStyleSingle
            .SpecialEffect = fmSpecialEffectFlat
        End With
        
        aa = (i - 1) Mod sizer
        If aa = 0 Then
            y = 1
            z = z + 1
        Else
            y = y + 1
        End If
        
        For j = 35 To 77
            myval = Cells(j, i).Value
            On Error GoTo ErrHandler:
            If myval >= 0.5 Then
                Cells(j, i).Interior.Color = RGB(0, 255, 0)
                If Cells(j, 1).Value <> Cells(34, i).Value Then
                    ReDim Preserve relA(k)
                    ReDim Preserve relB(k)
                    relA(k) = Cells(j, 1).Value
                    relB(k) = Cells(34, i).Value
                    listvar = Cells(j, 1).Value
                    MyListBox1.ControlFormat.AddItem listvar
                    k = k + 1
                End If
            ElseIf myval <= -0.5 Then
                Cells(j, i).Interior.Color = RGB(0, 255, 0)
                If Cells(j, 1).Value <> Cells(34, i).Value Then
                    ReDim Preserve relA(k)
                    ReDim Preserve relB(k)
                    relA(k) = Cells(j, 1).Value
                    relB(k) = Cells(34, i).Value
                    listvar = Cells(j, 1).Value
                    MyListBox1.ControlFormat.AddItem listvar
                    k = k + 1
                End If
            End If
    Ret1:
        Next j
    Next i
    
    VBA.UserForms.Add(TempForm.Name).Show
    
    ErrHandler:
        If i < 45 Then
            If Err <> 13 Then
                MsgBox "The most recent error number is " & Err & _
                ". Its message text is: " & Error(Err)
            End If
            Resume Ret1:
        Else
            Exit Sub
        End If
    
    ' End Data Analysis Section -------------------------------------
    
    
    
    End Sub
    The error is triggered by the AddItem code in my looped if statements.

    Thanks,

    ~J

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,487

    Re: Error 424 Object Required Error when Using AddItem to ListBox Generated in Code

    Are you not mixing runtime code with design time code?

    Your code creates the form and controls at design time, yes?

    If so then the Additem is really a runtime operation.
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1