Hi All,
I tried posting this before, but it failed because I did not tag it
I am using a combobox above a list box. The combobox has a list of 5 categories. Each category has few fields associated to it.
The category list and the fields associated with each category are populated in a worksheet with a named range.
I have two list boxes.The first list box rowsource is based on the selection of the combobox category.
When I bring up the form, the list box is populated automatically, which I am confused, since the rowsource is set based on combobox.
When I change the category, the list box gets populated but fails when I try to add items.
I have a ADD and a Remove button to add or remove items between two list boxes.
When I try to add items to the second list box, it fails at the RemoveItem i line.
[
.....
Me.lstBox1.RemoveItem i
...
]
Option Explicit
'Move ListBox Items code
Private Sub cboChooseFields_Change()
If cboChooseFields.Value = "Frequently Used Fields" Then
lstBox1.RowSource = "CDRFreqList"
ElseIf cboChooseFields.Value = "All Fields" Then
lstBox1.RowSource = "CDRALLFieldsList"
ElseIf cboChooseFields.Value = "IP Address Fields" Then
lstBox1.RowSource = "CDRIPAddrFieldsList"
ElseIf cboChooseFields.Value = "Date/Time Fields" Then
lstBox1.RowSource = "CDRDateTimeList"
ElseIf cboChooseFields.Value = "Partition and Number Fields" Then
lstBox1.RowSource = "CDRPartitionList"
End If
End Sub
Private Sub LoadStateListBox(Optional LstBox As String = "All")
Dim cel As Range
Dim i As Integer
Dim disableOption As Boolean
Select Case LstBox
Case "DeviceType01", "All"
Me.lstBox1.Clear
For Each cel In Range("DeviceType01").CurrentRegion
If cel.Value <> "DeviceType01" Then Me.lstBox1.AddItem cel.Value
Next cel
End Select
Select Case LstBox
Case "DeviceType02", "All"
Me.lstBox2.Clear
For Each cel In Range("DeviceType02").CurrentRegion
If cel.Value <> "DeviceType02" Then Me.lstBox2.AddItem cel.Value
Next cel
End Select
End Sub
Private Sub StateOptionClick(ab As String)
Dim i As Integer
Dim cel As Range
' Range("DeviceType01").CurrentRegion.ClearContents
' Range("Devicetype01" & ab).CurrentRegion.Copy Range("DeviceType01")
Range("DeviceType01").Value = "DeviceType01"
LoadStateListBox
For i = Me.lstBox2.ListCount - 1 To 0 Step -1
Set cel = Range("DeviceType01").CurrentRegion.Find(What:=lstBox2.List(i), _
after:=Range("DeviceType01"), MatchCase:=True, LookAt:=xlWhole)
If cel Is Nothing Then
Else
lstBox1.RemoveItem cel.Row - 2
Range("DeviceType01").Offset(cel.Row - 1, 0).Delete Shift:=xlUp
End If
Next i
End Sub
Private Sub cmdAdd_Click()
Dim i As Integer
Application.EnableEvents = False
Application.ScreenUpdating = False
If Me.lstBox1.ListIndex = -1 Then Exit Sub
For i = Me.lstBox1.ListCount - 1 To 0 Step -1
If Me.lstBox1.Selected(i) = True Then
' Add, sort and load
Me.lstBox2.AddItem Me.lstBox1.List(i)
With Range("DeviceType02")
.Offset(1, 0).Insert Shift:=xlDown
.Offset(1, 0).Value = Me.lstBox1.List(i)
.CurrentRegion.Sort Key1:=Range("DeviceType02"), Order1:=xlAscending, Header:=xlYes
End With
' Remove from list
Range("DeviceType01").Offset(i + 1, 0).Delete Shift:=xlUp
Me.lstBox1.RemoveItem i
'Reload to place in sort order
LoadStateListBox "DeviceType02"
End If
Next i
'DisplayFr
Application.EnableEvents = True
End Sub
Private Sub cmdAddAll_Click()
Dim i As Long
With Me.lstBox1
'Loop through the items
For i = 0 To .ListCount - 1
'Add each item to the other listbox
Me.lstBox2.AddItem .List(i)
Next i
'Clear the items from the current listbox
.Clear
End With
End Sub
Private Sub cmdCancel_Click()
Me.Tag = "Cancel"
Me.Hide
End Sub
Private Sub cmdMoveDown_Click()
If Me.lstBox2.ListCount = 0 Then Exit Sub
If Me.lstBox2.ListIndex = Me.lstBox2.ListCount - 1 Then Exit Sub
Dim i As Long, j As Long, valtoadd As String, bl As Boolean
bl = False
For i = 0 To Me.lstBox2.ListCount - 1
If Me.lstBox2.Selected(i) = True Then
valtoadd = Me.lstBox2.Column(0, i)
Me.lstBox2.RemoveItem (i)
bl = True
j = i + 1
End If
Next
If bl = False Then Exit Sub
Me.lstBox2.AddItem valtoadd, j
Me.lstBox2.ListIndex = j
End Sub
Private Sub cmdMoveUp_Click()
If Me.lstBox2.ListCount = 0 Then Exit Sub
If Me.lstBox2.ListIndex = 0 Then Exit Sub
Dim i As Long, j As Long, valtoadd As String, bl As Boolean
bl = False
For i = 0 To Me.lstBox2.ListCount - 1
If Me.lstBox2.Selected(i) = True Then
valtoadd = Me.lstBox2.Column(0, i)
Me.lstBox2.RemoveItem (i)
bl = True
j = i - 1
End If
Next
If bl = False Then Exit Sub
Me.lstBox2.AddItem valtoadd, j
Me.lstBox2.ListIndex = j
End Sub
Private Sub cmdRem_Click()
Dim i As Integer
Application.EnableEvents = False
Application.ScreenUpdating = False
If Me.lstBox2.ListIndex = -1 Then Exit Sub
For i = Me.lstBox2.ListCount - 1 To 0 Step -1
If Me.lstBox2.Selected(i) = True Then
' Add, sort and load
Me.lstBox1.AddItem Me.lstBox2.List(i)
With Range("DeviceType01")
.Offset(1, 0).Insert Shift:=xlDown
.Offset(1, 0).Value = Me.lstBox2.List(i)
.CurrentRegion.Sort Key1:=Range("DeviceType01"), Order1:=xlAscending, Header:=xlYes
End With
' Remove from list
Range("DeviceType02").Offset(i + 1, 0).Delete Shift:=xlUp
Me.lstBox2.RemoveItem i
'Reload to place in sort order
LoadStateListBox "DeviceType01"
End If
Next i
'DisplayFr
Application.EnableEvents = True
End Sub
Private Sub cmdRemAll_Click()
Dim i As Long
With Me.lstBox2
For i = 0 To .ListCount - 1
Me.lstBox1.AddItem .List(i)
Next i
.Clear
End With
End Sub
Private Sub UserForm_Activate()
StateOptionClick "b"
End Sub
Display More
I guess, the list box should be cleared,but I have no idea.
Thanks,