combobox clearing

angietate1

New Member
Joined
Feb 14, 2016
Messages
1
My comboboxes keep clearing. I'm a new user to comboboxes and VBA. The three drop downs are cascading and they clear to soon or they won't stay selected. Help! Is there a way to attach the document?

Private Sub Combobox21_change()
Dim wsData As Worksheet
Dim listOfValues As String 'To store list of values already added
Dim ValueToAdd As String 'To store new value to add
listOfValues = ""
Set wsData = ThisWorkbook.Sheets("DATA")


MyVal = Me.ComboBox21.Value


'loop thru col a
lr = ThisWorkbook.Sheets("data").Cells(Rows.Count, 1).End(xlUp).Row


'clear combobox 22
Me.ComboBox22.Clear


'loop thru
For x = 2 To lr
If MyVal = wsData.Cells(x, 1) Then
'add to combobox
ValueToAdd = wsData.Cells(x, 2) 'Get value from worksheet
If InStr(listOfValues, ValueToAdd) = 0 Then
'Check to see if the value has already been added
'If not, add to values added and add the item to the combobox.
listOfValues = listOfValues & ValueToAdd
Me.ComboBox22.AddItem ValueToAdd
End If
End If
Next x

End Sub


Private Sub ComboBox22_Change()
Dim wsData As Worksheet
Dim listOfValues As String 'To store list of values already added
Dim ValueToAdd As String 'To store new value to add
listOfValues = ""
Set wsData = ThisWorkbook.Sheets("DATA")


MyVal = Me.ComboBox22.Value


'loop thru col b
lr = ThisWorkbook.Sheets("data").Cells(Rows.Count, 1).End(xlUp).Row


'clear combobox 23
Me.ComboBox23.Clear


'loop thru
For x = 2 To lr
If MyVal = wsData.Cells(x, 2) Then
'add to combobox
ValueToAdd = wsData.Cells(x, 3) 'Get value from worksheet
If InStr(listOfValues, ValueToAdd) = 0 Then
'Check to see if the value has already been added
'If not, add to values added and add the item to the combobox.
listOfValues = listOfValues & ValueToAdd
Me.ComboBox23.AddItem ValueToAdd
End If
End If
Next x

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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