UserForm with combobox and listbox depending on this

chris1983

New Member
Joined
Mar 23, 2020
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone!

I created a userform with a combobox and a listbox. The idea is to choose from the combobox, and depending on the choice, to choose from a specific list in the listbox and use that value later on.

So what i have done so far, is the following event with Combobox change:

VBA Code:
Private Sub ComboBox1_Change()

Dim i, n As Integer
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")

n = Application.WorksheetFunction.Match(Me.ComboBox1.Value, sh.range("1:1"), 0)
Me.ListBox1.Clear
For i = 2 To Application.WorksheetFunction.CountA(sh.Cells(1, n).EntireColumn)
    Me.ListBox1.AddItem sh.Cells(i, n).Value
 Next i

End Sub

After making a choice in combox ,the listbox shows all the items correctly. I added a command button to run the following.

Code:
Private Sub CommandButton_Click()

dim s as varient
If Me.ListBox1.ListIndex > -1 Then
s = Me.ListBox1.Value
End If

End Sub

And here is the problem. I want to use the value of the selected item in listbox but i get an error. (Run-time error '94', Invalid use of Null). The listindex however is correct.

Any ideas? Thanks in advance
 
You're welcome & thanks for the feedback
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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