ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Add item to listbox, but only if it is not already there. (https://www.excelbanter.com/excel-programming/339088-add-item-listbox-but-only-if-not-already-there.html)

Mark

Add item to listbox, but only if it is not already there.
 
I am trying to write code so that if an item is selected in one listbox, it
will be added to another listbox. However, it should only be added if it
does not already exist in the second listbox.

Why is the following code doing nothing. It make logical sense, to me.

For i = 0 To UserForm1.ColumnSelect.ListCount - 1
If UserForm1.ColumnSelect.Selected(i) Then
For j = 0 To UserForm1.ColFilter.ListCount
If UserForm1.ColumnSelect.List(i) <
UserForm1.ColFilter.List(j) Then
UserForm1.ColFilter.AddItem UserForm1.ColumnSelect.List(i)
End If
Next j
End If
Next i

Bob Phillips[_6_]

Add item to listbox, but only if it is not already there.
 


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Mark" wrote in message
...
I am trying to write code so that if an item is selected in one listbox,

it
will be added to another listbox. However, it should only be added if it
does not already exist in the second listbox.

Why is the following code doing nothing. It make logical sense, to me.

For i = 0 To UserForm1.ColumnSelect.ListCount - 1
If UserForm1.ColumnSelect.Selected(i) Then
For j = 0 To UserForm1.ColFilter.ListCount
If UserForm1.ColumnSelect.List(i) <
UserForm1.ColFilter.List(j) Then
UserForm1.ColFilter.AddItem

UserForm1.ColumnSelect.List(i)
End If
Next j
End If
Next i




Bob Phillips[_6_]

Add item to listbox, but only if it is not already there.
 
Dim i As Long
Dim j As Long
Dim fOK As Boolean

With Me
For i = 0 To .ColumnSelect.ListCount - 1
If .ColumnSelect.Selected(i) Then
fOK = True
For j = 0 To .ColFilter.ListCount - 1
If .ColumnSelect.List(i) = .ColFilter.List(j) Then
fOK = False
Exit For
End If
Next j
If fOK Then
.ColFilter.AddItem .ColumnSelect.List(i)
Else
MsgBox .ColumnSelect.List(i) & _
" already in list"
End If
End If
Next i
End With

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Mark" wrote in message
...
I am trying to write code so that if an item is selected in one listbox,

it
will be added to another listbox. However, it should only be added if it
does not already exist in the second listbox.

Why is the following code doing nothing. It make logical sense, to me.

For i = 0 To UserForm1.ColumnSelect.ListCount - 1
If UserForm1.ColumnSelect.Selected(i) Then
For j = 0 To UserForm1.ColFilter.ListCount
If UserForm1.ColumnSelect.List(i) <
UserForm1.ColFilter.List(j) Then
UserForm1.ColFilter.AddItem

UserForm1.ColumnSelect.List(i)
End If
Next j
End If
Next i




Mark

Add item to listbox, but only if it is not already there.
 
Thanks. That definitely did it!

"Bob Phillips" wrote:

Dim i As Long
Dim j As Long
Dim fOK As Boolean

With Me
For i = 0 To .ColumnSelect.ListCount - 1
If .ColumnSelect.Selected(i) Then
fOK = True
For j = 0 To .ColFilter.ListCount - 1
If .ColumnSelect.List(i) = .ColFilter.List(j) Then
fOK = False
Exit For
End If
Next j
If fOK Then
.ColFilter.AddItem .ColumnSelect.List(i)
Else
MsgBox .ColumnSelect.List(i) & _
" already in list"
End If
End If
Next i
End With

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Mark" wrote in message
...
I am trying to write code so that if an item is selected in one listbox,

it
will be added to another listbox. However, it should only be added if it
does not already exist in the second listbox.

Why is the following code doing nothing. It make logical sense, to me.

For i = 0 To UserForm1.ColumnSelect.ListCount - 1
If UserForm1.ColumnSelect.Selected(i) Then
For j = 0 To UserForm1.ColFilter.ListCount
If UserForm1.ColumnSelect.List(i) <
UserForm1.ColFilter.List(j) Then
UserForm1.ColFilter.AddItem

UserForm1.ColumnSelect.List(i)
End If
Next j
End If
Next i





Mark

Add item to listbox, but only if it is not already there.
 
What if I were trying to remove an item that was in the second box?


Private Sub cRemove_Click()

' Remove items that were added to the ColFilter list box, but which
' are no longer desired as part of the report

Dim i As Long
Dim j As Long
Dim fOK As Boolean

With Me
For i = 0 To .ColumnSelect.ListCount - 1
If .ColumnSelect.Selected(i) Then
fOK = False
For j = 0 To .ColFilter.ListCount - 1
If .ColumnSelect.List(i) < .ColFilter.List(j) Then
fOK = True
Exit For
End If
Next j
If fOK Then
.ColFilter.RemoveItem .ColumnSelect.List(i)
Else
MsgBox (.ColumnSelect.List(i) & " is not part of the report
columns.")
End If
End If
Next i
End With

End Sub

"Mark" wrote:

Thanks. That definitely did it!

"Bob Phillips" wrote:

Dim i As Long
Dim j As Long
Dim fOK As Boolean

With Me
For i = 0 To .ColumnSelect.ListCount - 1
If .ColumnSelect.Selected(i) Then
fOK = True
For j = 0 To .ColFilter.ListCount - 1
If .ColumnSelect.List(i) = .ColFilter.List(j) Then
fOK = False
Exit For
End If
Next j
If fOK Then
.ColFilter.AddItem .ColumnSelect.List(i)
Else
MsgBox .ColumnSelect.List(i) & _
" already in list"
End If
End If
Next i
End With

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Mark" wrote in message
...
I am trying to write code so that if an item is selected in one listbox,

it
will be added to another listbox. However, it should only be added if it
does not already exist in the second listbox.

Why is the following code doing nothing. It make logical sense, to me.

For i = 0 To UserForm1.ColumnSelect.ListCount - 1
If UserForm1.ColumnSelect.Selected(i) Then
For j = 0 To UserForm1.ColFilter.ListCount
If UserForm1.ColumnSelect.List(i) <
UserForm1.ColFilter.List(j) Then
UserForm1.ColFilter.AddItem

UserForm1.ColumnSelect.List(i)
End If
Next j
End If
Next i






All times are GMT +1. The time now is 08:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com