Userform Activate listbox in VBA

L

Legacy 452653

Guest
I have a warehouse module that I am completing and have 22 listboxes that consist of 22 pick lines to click on.
With this in mind, I have created a Userform Activate that when ever an order has been raised it opens a "check order" userform and the VBA code below selects all 22 listboxes to verify any lines that are in the "RED".

However, if there is no data past "lstLookup", It indicates Run-time error '380', could not set the selected property. Invalid property value.

Is there a VBA coding that if there was no "data" or "Value" in any listbox to continue with the opening of the userform

Private Sub UserForm_Activate()
'Dim arrItems() As String
Dim bRB As MSForms.ReturnBoolean
lstLookup.Selected(0) = True
lstLookup_DblClick bRB
lstLookup1.Selected(0) = True
lstLookup1_DblClick bRB

I have put a "comma" in front of LstLookup6 to verify if there is no data in this list and sure enough debug indicate No7
'lstLookup6.Selected(0) = True
'lstLookup6_DblClick bRB

Thanks

Steve
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi Steve,

The error message you are receiving is because you're trying to select an item that doesn't exist. You can test for an empty ListBox using something like the following:
VBA Code:
    If lstLookup.ListCount < 1 Then
        'The ListBox is empty
    End If


So if you wanted, for example, to select the first list item in a none-empty ListBox control you could use:
VBA Code:
    If Not lstLookup.ListCount < 1 Then
        lstLookup.selected(0) = True
    End If


To do it for all ListBox controls you could do something like the following (though you may need to tweak the code a bit to fit your particular circumstances):
VBA Code:
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "ListBox" Then
            If Not ctrl.ListCount < 1 Then
                ctrl.selected(0) = True
            End If
        End If
    Next
 
Upvote 0
Thanks for that Sunjinsak,

I prefer the 2nd code. That works well!!
I did the last one and see no listbox selected. Maybe as you had indicated to modify or tweak it or maybe I did something wrong.
I will look at it later hopfully it will work on the other userform.

Thanks anyway

Steve
 
Upvote 0
There is a order userform that populates each line to check order userform and each line as a name range into list box. Any lines that are duplicate and or QTY ordered is over it will show a conditional format in red. Then I can delete those lines and create the order....this is fail safe from the previous version that someone else build and it wasn't correcting itself. So I made an addition userform that checks automatically.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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