Select Row based upon selection in Multiselect ListBox using VBA

pmpatel189

New Member
Joined
Jun 17, 2017
Messages
4
I have Part IDs in Column C and my userform has Listbox containing Part IDs.

If I select Part ID in Listbox then whole row containing that PartID should be selected.

My Listbox is multiselect. So if I select multiple IDs then it should select corresponding multiple rows
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:-
Nb:- Do not use the "RowSource" property, the code will fill the listbox with column "C".
Nb:- Place this code in the Userform Module.

Code:
Option Explicit
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object
Private [COLOR="Navy"]Sub[/COLOR] ListBox1_Change()
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]With[/COLOR] ListBox1
[COLOR="Navy"]For[/COLOR] n = 0 To .ListCount - 1
    [COLOR="Navy"]If[/COLOR] .Selected(n) [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Set[/COLOR] Rng = _
        Dic(.List(n)) Else [COLOR="Navy"]Set[/COLOR] Rng = Union(Rng, Dic(.List(n)))
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] With
 [COLOR="Navy"]If[/COLOR] Not Rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
    Rng.EntireRow.Select
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] UserForm_Initialize()
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C1", Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]Set[/COLOR] Dic(Dn.Value) = Dn
[COLOR="Navy"]Next[/COLOR]
 [COLOR="Navy"]With[/COLOR] Me.ListBox1
    .List = Application.Transpose(Dic.Keys)
    .MultiSelect = fmMultiSelectMulti
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Option Explicit
Dim Dic As Object


Private Sub CommandButton1_Click()


Dim Rng As Range, Dn As Range, n As Long, cpart As Range, ws As Worksheet






Set Rng = Range("C1", Range("C" & Rows.Count).End(xlUp))
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
For Each Dn In Rng
Set Dic(Dn.Value) = Dn
Next
With Me.ListBox1
.List = Application.Transpose(Dic.Keys)
.MultiSelect = fmMultiSelectMulti
End With




End Sub


Private Sub ListBox1_Change()
Dim n As Long, Rng As Range


With ListBox1
For n = 0 To .ListCount - 1
If .Selected(n) Then
If Rng Is Nothing Then Set Rng = Dic(.List(n))
Else
Set Rng = Union(Rng, Dic(.List(n)))
End If
Next n
End With
If Not Rng Is Nothing Then
Rng.EntireRow.Select
End If
End Sub
Private Sub UserForm_Initialize()
Dim cpart As Range, ws As Worksheet


Range("D8", Range("D" & Rows.Count).End(xlUp)).Name = "Dynamic"
Me.ListBox1.RowSource = "Dynamic"




End Sub


Mick I modified code to work with button. But shows Run-time error Object variable or with block variable not set
 
Upvote 0
You don't need to do that, please see my original notes

Nb:- Do NOT use the "RowSource" property, the code will fill the listbox with column "C".
Nb:- Place this code in the Userform Module.

The code is written to load columns "C" data to the listbox , and by using "The dictionary", it creates a reference to the address of those cells.
You specifying a "Rowsource" will not let that happen.
I also see your "Rowsource" is shown as column "D" not column "C",as you previously specified ?????
 
Upvote 0
I thought i can correct myself but i am totally new to listbox so i couldnt.

Actually i want to fetch p/ns from sheet1 and there are labels for p/ns in sheet2. So basically what i want is list items should fetch p/ns. data dynamically from sheet1 and when i select p/ns. from list box and press button it should select row
For that p/ns.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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