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
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,139
Office Version
  1. 365
Platform
  1. Windows
Is your listbox single or multi select?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,139
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

In that case do you want to get all the selected values?
If so can there be duplicates?
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,380
Office Version
  1. 2019
Platform
  1. Windows
Unlikely to solve your issue but couple of observations

  • need to correct spelling of your variables data type
Rich (BB code):
dim s as varient

to this

VBA Code:
Dim s As Variant

Also,

This line

Rich (BB code):
Dim i, n As Integer

Variable i declared this way is a Variant not an Integer.

When declaring variables even on same line you must explicitly declare the data type for each

VBA Code:
Dim i As Long, n As Long

dealing with Rows, it is safer to declare as Long data type as rows > 32767

Hope Helpful

Dave
 
Last edited:

chris1983

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

ADVERTISEMENT

Unlikely to solve your issue but couple of observations

  • need to correct spelling of your variables data type
Rich (BB code):
dim s as varient

to this

VBA Code:
Dim s As Variant

Also,

This line

Rich (BB code):
Dim i, n As Integer

Variable i declared this way is a Variant not an Integer.

When declaring variables even on same line you must explicitly declare the data type for each

VBA Code:
Dim i As Long, n As Long

dealing with Rows, it is safer to declare as Long data type as rows > 32767

Hope Helpful

Dave
Thanks for your comments!
 

chris1983

New Member
Joined
Mar 23, 2020
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
In that case do you want to get all the selected values?
If so can there be duplicates?
I have already checked for duplicates when adding the values for the list, there are no any.
Yes, it would be great to get all the selected values
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,139
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Private Sub CommandButton2_Click()
   Dim i As Long
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Me.ListBox1
      For i = 0 To .ListCount - 1
         If .Selected(i) Then Dic(.List(i)) = Empty
      Next i
   End With
   MsgBox Join(Dic.keys, Chr(10))
End Sub
 

chris1983

New Member
Joined
Mar 23, 2020
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
How about
VBA Code:
Private Sub CommandButton2_Click()
   Dim i As Long
   Dim Dic As Object
  
   Set Dic = CreateObject("scripting.dictionary")
   With Me.ListBox1
      For i = 0 To .ListCount - 1
         If .Selected(i) Then Dic(.List(i)) = Empty
      Next i
   End With
   MsgBox Join(Dic.keys, Chr(10))
End Sub
It seems to work fine! Thank you very much!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,995
Messages
5,599,256
Members
414,299
Latest member
thenewworld

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
Top