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
Bookmarks