VBA bypass ListBox (or other control object) to sub

gifariz

Board Regular
Joined
May 2, 2021
Messages
112
Office Version
  1. 365
Platform
  1. Windows
Hi all. How to bypass a control object (e.g. ListBox) to another sub/function?

For example, what I try here below gives Type Mismatch error when bypassing ListBox1 to SetSelectedListBox sub.
VBA Code:
'Select All ListBox1
Private Sub CommandButton3_Click()
    SetSelectedListBox ListBox1, True
End Sub

'Clear All ListBox2
Private Sub CommandButton4_Click()
    SetSelectedListBox ListBox1, False
End Sub

'Select All ListBox2
Private Sub CommandButton5_Click()
    SetSelectedListBox ListBox2, True
End Sub

'Clear All ListBox2
Private Sub CommandButton6_Click()
    SetSelectedListBox ListBox2, False
End Sub

Private Sub SetSelectedListBox(inListBox As ListBox, IsSelected As Boolean)
    Dim i As Long
    For i = 0 To inListBox.ListCount - 1
        inListBox.Selected(i) = IsSelected
    Next i
End Sub

TIA
 

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
If not fully qualified, ListBox refers by default to Excel Forms ListBoxes not ActiveX listboxes.

This should work:
SetSelectedListBox(inListBox As MsForms.ListBox, IsSelected As Boolean)

ALternatively, you could use inListBox As Object or As Variant
 
Upvote 1
Solution
If not fully qualified, ListBox refers by default to Excel Forms ListBoxes not ActiveX listboxes.

This should work:
SetSelectedListBox(inListBox As MsForms.ListBox, IsSelected As Boolean)

ALternatively, you could use inListBox As Object or As Variant
Thanks a lot for your solution and explanation. I understand now.
I will use the MsForms.ListBox option for clarity and early binding.
 
Upvote 0

Forum statistics

Threads
1,215,537
Messages
6,125,386
Members
449,221
Latest member
DFCarter

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