Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox selecting and deselecting items in listbox

Status
Not open for further replies.

londonchef

Programmer
Dec 17, 2003
2
0
0
GB
Hi,

If anyone can help that would be fantastic.

My problem is this: I have a userform with a combobox, listbox and a textbox. When an item/items is selected from the listbox it is then displayed in the textbox. When I deselect the items from the listbox, the items also disappear from the textbox (which is what I want it to do). Now when a category is selected from the combobox I have a piece of code (Preselect) that re-populates the listbox which then is displayed in the textbox. However this works fine when I first select a category from the combobox but when I select another different category the listbox does not get reset so it ends up displaying the new selection as well as the old selection which is also displayed in the textbox.

I have tried setting the listindex to –1 but this still does not work. The code I have used including the preselect code is below.

Please help!

Rich



Private Sub ComboBox2_Change()

Application.ScreenUpdating = False

Sheets("Eventsdatabase").Select
Range("eventsdatabase!AR1:AS10").ClearContents
UserForm1.TextBox1 = " "
With Worksheets("eventsdatabase")
UserForm1.TextBox1 = .[ar13]

Sheets("Eventsdatabase").Cells(3, 2).Value = UserForm1.ComboBox2.Value

Range("eventsdatabase!L3").ClearContents

Application.ScreenUpdating = True

lookup

UpdateRow2

UserForm1.ListBox1.ListIndex = -1

Preselect


End Sub


Sub ListBox1_Change()

Dim i As Long
Dim iDestRow As Long

' Delete any existing data in output range
Range("EventsDatabase!AR1:AR10").ClearContents

iDestRow = 1
With UserForm1.ListBox1
' Loop thru listbox items and check the selected property
' if it is set then we write the value to next row available
' in the output range.
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
' If you have more than one column
' in your list box you will have to specify
' which column as well as the row
Sheets("EventsDatabase").Cells(iDestRow, 44) = .List(i, 0)
iDestRow = iDestRow + 1
Else
End If
If ComboBox1.ListIndex = 0 Then
If Range("Event1!gd32") > 0 And Range("Event1!gd37") = False Then
UserForm4.Show
End If

End If

Next
End With

If Range("Eventsdatabase!aw11") > 0 Then
IAnswer = MsgBox("You have selected the same product to move share from and to. Please Reselect", vbOKOnly, "Same Product")
If IAnswer = vbOK Then
Range("EventsDatabase!AR1:AS10").ClearContents
UserForm1.ListBox1.ListIndex = -1
UserForm1.TextBox1 = " "
UserForm1.TextBox7 = " "
End If
End If

Sheets("eventsdatabase").Calculate
With Worksheets("eventsdatabase")
UserForm1.TextBox1 = .[ar13]
End With


If ComboBox1.ListIndex = 0 Then
Range("EventsDatabase!L3").ClearContents
Range("Eventsdatabase!n3").ClearContents
Sheets("eventsdatabase").Calculate
ElseIf ComboBox1.ListIndex = 1 Then
Range("eventsdatabase!h3:i3").ClearContents
Range("eventsdatabase!K3").ClearContents
Range("eventsdatabase!N3").ClearContents
End If

End Sub


Sub Preselect()

Dim strFrom() As String
Dim intItems As Integer
Dim intCounter As Integer
Dim i As Integer
Dim rngFrom As Excel.Range

Worksheets("EventsDatabase").Select
UserForm1.ListBox1.ListIndex = -1
Range("eventsdatabase!ar1:as10").ClearContents
Set rngFrom = Range("From_List")
intItems = rngFrom.Columns.Count - 1
ReDim strFrom(intItems)

'Populate array variable
For intCounter = 0 To intItems
strFrom(intCounter) = rngFrom.Cells(1, intCounter).Value


Next intCounter

For intCounter = 0 To intItems
For i = 0 To UserForm1.ListBox1.ListCount - 1

'Debug.Print i & " " & UserForm1.ListBox1.List(i)
If strFrom(intCounter) = UserForm1.ListBox1.List(i) Then
UserForm1.ListBox1.Selected(i) = True
End If
Next i
Next intCounter

End Sub
 
Have you tried using the clear method prior to repopulating?

UserForm1.ListBox1.Clear
 
Hi jyotko

Thanks for your reply. The listbox I have always needs to be populated with the same data. However I just need to select different items in the same list when I change categories in the combobox. I will give it a go. Also I've just stumbled across this bit of code below on the forum which I will try as well.

Cheers

Rich

Dim lCtl_CntrlSource As Control
Dim lInt_CurrRow As Integer

Set lCtl_CntrlSource = UserForm1.ListBox1
For lInt_CurrRow = 0 To lCtl_CntrlSource.ListCount - 1
lCtl_CntrlSource.Selected(lInt_CurrRow) = False
Next lInt_CurrRow

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top