londonchef
Programmer
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
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"
Range("eventsdatabase!AR1:AS10"
UserForm1.TextBox1 = " "
With Worksheets("eventsdatabase"
UserForm1.TextBox1 = .[ar13]
Sheets("Eventsdatabase"
Range("eventsdatabase!L3"
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"
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"
iDestRow = iDestRow + 1
Else
End If
If ComboBox1.ListIndex = 0 Then
If Range("Event1!gd32"
UserForm4.Show
End If
End If
Next
End With
If Range("Eventsdatabase!aw11"
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"
UserForm1.ListBox1.ListIndex = -1
UserForm1.TextBox1 = " "
UserForm1.TextBox7 = " "
End If
End If
Sheets("eventsdatabase"
With Worksheets("eventsdatabase"
UserForm1.TextBox1 = .[ar13]
End With
If ComboBox1.ListIndex = 0 Then
Range("EventsDatabase!L3"
Range("Eventsdatabase!n3"
Sheets("eventsdatabase"
ElseIf ComboBox1.ListIndex = 1 Then
Range("eventsdatabase!h3:i3"
Range("eventsdatabase!K3"
Range("eventsdatabase!N3"
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"
UserForm1.ListBox1.ListIndex = -1
Range("eventsdatabase!ar1:as10"
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