On ListBox to be selected items from range

Sahak

Well-known Member
Joined
Nov 10, 2006
Messages
1,008
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Hi all,

I have a listBox on user form & It has RowSource = Range A1:A40, where there are following Items:
GGG
BBB
SSS
CCC
UUU
ZZZ
& more

When I activate Form, I would like on ListBox to be selected Items listed in range M1:M4, let say in range we have BBB & UUU , so on ListBox should be selected only BBB & UUU.

Thank you in advance
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Why do you say:
Range A1:A40

And then say
range M1:M4
<strike>
</strike>
 
Upvote 0
Hi Sahak,

Make sure the MultiSelect property of your listbox is set to fmMultiSelectMulti and change the name of the listbox in the following to suit (if necessary):

Code:
Option Explicit
Private Sub UserForm_Activate()

    Dim intListBoxIndex As Integer
    Dim lngMatchRowNo   As Long
    
    With Me.ListBox1 '<- Change to the name of your listbox
        For intListBoxIndex = 0 To .ListCount - 1
            On Error Resume Next
                lngMatchRowNo = Evaluate("MATCH(""" & .List(intListBoxIndex) & """,$M$1:$M$4,0)")
                If lngMatchRowNo > 0 Then
                    .Selected(intListBoxIndex) = True
                End If
                lngMatchRowNo = 0
            On Error GoTo 0
        Next intListBoxIndex
    End With

End Sub

Regards,

Robert
 
Upvote 0
MultySelect ListBox's RowSource from Range A1:A40 (Item list comes from there) I would like to be selected (highlited) only items from range M1:Mk
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,957
Latest member
Hat4Life

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