Passing Object Error

Jonas Offersen

New Member
Joined
Feb 13, 2018
Messages
14
So, this code isn't working for me. For whatever reason, I get an " Error 424 - object required" error, when I try to pass the object into the sub.
I do not get this error when I try to use it outside of the function.

Code:
Private Sub Form_Load()
    Dim obj As ListBox
    Set obj = Me.ListNiveau
    obj.visible = False [COLOR=#0000ff]'This is working![/COLOR]
    LayoutControls.SetListBoxColWidths (obj) [COLOR=#0000ff]'This returns error 424.[/COLOR]
End Sub

The code bellow is altering the textbox columns as working as intended.

Code:
Public Sub SetListBoxColWidths(ListBoxName As ListBox)

    Dim i As Integer
    Dim j As Integer

    Dim ColMaxWidth(2) As Double ' for 3 column listbox
   [COLOR=#0000ff]
'Initialise to zero[/COLOR]
    For j = 0 To 2
        ColMaxWidth(j) = 0
    Next j
   [COLOR=#0000ff]
'itereate thru each item in listbox and find max size[/COLOR]
    For i = 0 To ListBoxName.ListCount - 1
        For j = 0 To 2
            If ColMaxWidth(j) < (Len(ListBoxName.Column(j, i)) * 125) Then
                ColMaxWidth(j) = (Len(ListBoxName.Column(j, i)) * 125)
            End If
        Next j
    Next i
[COLOR=#0000ff]
'display columnwidths based on max size[/COLOR]
    ListBoxName.ColumnWidths = ColMaxWidth(0) & ";" & ColMaxWidth(1) & ";" & ColMaxWidth(2) & ""

End Sub

As it turns out I don't actually need this right now. But I would like to know what I'm doing wrong regardless, for next time.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You need to remove the parentheses - you only need those when passing an argument to a subroutine if you are using Call or if you are specifically trying to dereference an object - so it would just be:

Code:
LayoutControls.SetListBoxColWidths obj

or, with Call (which many regard as obsolete):

Code:
Call LayoutControls.SetListBoxColWidths(obj)

Note: when using a Function, you additionally use parentheses if you are using the result of the function.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top