Userform Listbox Help: Without using Command Buttons

coach_veto

New Member
Joined
Jan 26, 2016
Messages
15
Hi,

So I recently made a userform listbox that references a list of countries I have. I have the following code pasted in Sheet1 so that when I click one of the cells referenced, the userform pops up.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
UserForm1.Show
End If
End Sub

My problem is that once the list pops up, I want to be able to select one of the country names from this list and have it populate that country name in the active cell. I want to do this without having to create and click a command button. I also would like the list to disappear once a selection has been made and the cell is populated.

What code would I need? Does it go in the sheet1 area or the userform code area?

Thanks guys!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
In the userform module try this:

Code:
Private Sub ListBox1_Change()
ActiveCell.Value = ListBox1.Value
Unload Me
End Sub
 
Last edited:
Upvote 0
You can do all that without VBA using Data Validation. If you have your country list in D1:D5 for example,

1) Select your A1:A10 range
2) From the Data tab, click on Data Validation
3) In the Allow: box, select List
4) Make sure In-Cell dropdown is checked
5) In the Source box, enter
=$D$1:$D$5
(or whatever your range is)

And you're done.

If you want, you can just list the countries in the Source box without saving them on the sheet. Just put them in the Source box separated by commas.

Let me know if this works for you.
 
Upvote 0
Try this:

Code:
'Worksheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
        If Len(Target.Value) = 0 Then
            UserForm1.Show
        End If
    End If
End Sub

'UserForm module

Private Sub ListBox1_Click()
    ActiveCell.Value = ListBox1.Value
End Sub
 
Last edited:
Upvote 0
Thank you guys, these solutions all work.

@EricW I chose not to use a simple drop down list from data validation because I need my column width to be small but the countries in this list have long names. So if used a normal drop down, the list would cut off the names unless I made the column bigger, which I don't want to do. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,291
Members
449,498
Latest member
Lee_ray

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