Assign a Named range to a Listbox using Macro

Hi Experts,

I am using the following code to search via a listbox

Sub MySearch()
frmListPicker.Initialize ActiveCell.Cells(2, 3), [List]
frmListPicker.Show
If Not frmListPicker.Cancelled Then
   ActiveCell.Value = frmListPicker.Value
Else
   Unload frmListPicker
End If
End Sub

Open in new window


I want the code the always populate the NamedRange "MyNmdrng" when it shows up.

Please advise !
Kanwal_No2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieVBA ExpertCommented:
What's the name of the listbox and where's it located?
0
Kanwal_No2Author Commented:
It is a huge file with long code and I don't know exactly where it is located. I find the following relevant.

Option Explicit

Public Value As String
Public Cancelled As Boolean

Private mList As Variant

Public Sub Initialize(ByVal Value As Variant, ByVal List As Variant)
    mList = List
    txtValue = Value
    Me.Value = Value
    LoadList
    
End Sub
Public Sub LoadList()

    Dim Value As Variant

    lbxList.Clear
    For Each Value In mList
    '    If SoundEx(Value) Like SoundEx(txtValue) & "*" Or LCase(Value) Like LCase(txtValue) & "*" Then
        If SoundEx(Value) Like "*" & SoundEx(txtValue) & "*" Or LCase(Value) Like "*" & LCase(txtValue) & "*" Then
            lbxList.AddItem Value
        End If
    Next Value

End Sub

Private Sub cmdCancel_Click()

    Cancelled = True
    Me.Hide

End Sub

Private Sub cmdOK_Click()

    If lbxList.ListIndex < 0 Then
        Value = lbxList.List(0)
    Else
        Value = lbxList.Value
    End If
    Me.Hide

End Sub

Private Sub lbxList_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

    cmdOK_Click

End Sub

Private Sub txtValue_Change()

    LoadList

End Sub

Open in new window

0
NorieVBA ExpertCommented:
I don't understand, you don't know where the listbox you want to populate is?

Also, doesn't the code you posted populate a listbox?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Kanwal_No2Author Commented:
Thanks Norie !
I posted the part which seemed close to me. Never have dealt with VBA forms earlier so please excuse my ignorance with them. Searched for listbox and got the above code. Posted the entire module here which I found on double clicking a VBA Form.

Regards
0
Kanwal_No2Author Commented:
Hi Norie,

I hope the above is the required code !
0
Martin LissOlder than dirtCommented:
Change the LoadList sub to this and see what happens

Public Sub LoadList()

    Dim value As Range
    Dim bFound As Boolean

    lbxList.Clear
    For Each value In Range("mList")
    '    If SoundEx(Value) Like SoundEx(txtValue) & "*" Or LCase(Value) Like LCase(txtValue) & "*" Then
        If SoundEx(value) Like "*" & SoundEx(txtValue) & "*" Or LCase(value) Like "*" & LCase(txtValue) & "*" Then
            lbxList.AddItem value
            bFound = True
        End If
    Next
    If Not bFound Then
        MsgBox "Found no values that sound like '" & txtValue & "'"
    End If
End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
Here's a workbook with a Soundex search that uses my code above.
Q-28683526.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kanwal_No2Author Commented:
Thanks a Lot Martin !
I will try to update asap !
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.