So I am new to the forms side of the VBA coding and I seem to be struggling a bit with this one.
What I have did was follow this tutorial:
https://www.excel-easy.com/vba/examples/multiple-list-box-selections.html
And I have adapted it in a way that suits my needs, but now I am having an issue or two that I do not understand how I can resolve.
The code in the tutorial adds two list boxes to a form and then the add button copy items from the first listbox to the second and the remove button removes items from the second listbox.
The problem is that you can add a specific item more than once, and considering I would like to use the values in the second listbox, this is a problem as I need only unique values.
The code below is what I have come up with so far, but I am getting an error:
Private Sub btn_Add_Filter_Click()
For i = 0 To lbx_Filters_List.ListCount - 1
If lbx_Filters_List.Selected(i) = True Then
For X = 0 To lbx_Filters.ListCount
If Not IsError(lbx_Filters.List(X)) Then
mVal = 0
If lbx_Filters.List(X) <> "" And lbx_Filters.List(X) = lbx_Filters_List.List(i) Then
myVal = 1
End If
End If
If myVal = 0 Then
lbx_Filters.AddItem _
lbx_Filters_List.List(i)
End If
Next X
End If
Next i
End Sub
The error occurs the second time I try and add the same item from the first listbox and what happens is that the second for loop will loop once and on the second loop it throws an error on this line:
If Not IsError(lbx_Filters.List(X)) Then
Error being:
Could not get the list property. Invalid property array index
x=1at the time of execution and I cannot get a value forlbx_Filters.List(X)as that is throwing the error.