ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding to a ListBox (https://www.excelbanter.com/excel-programming/391771-adding-listbox.html)

Pflugs

Adding to a ListBox
 
I have a userform that contains a listbox. Users can add values by entering
number in a textbox and clicking an "add" button. The problem is once a
certain number of entries have been added, the next values appear at the end
of the listbox.

Using the .AddItem method, how do I add values to the beginning of the list?
The code keeps saying it expects an "=" sign.

Code:
Private Sub cbCustomCodeAdd_Click()
With Me
If Not .tbCustomCode.Value = "" Then
For Each item In .lbGathCodes.List
If item = .tbCustomCode.Value Then
.tbCustomCode.Value = ""
Exit Sub
End If
Next item
If Len(.tbCustomCode) 3 Then .tbCustomCode =
Left(.tbCustomCode, 3)
.lbGathCodes.AddItem (.tbCustomCode.Value)
.tbCustomCode.Value = ""
.lbGathCodes.Selected(.lbGathCodes.ListCount - 1) = True
End If
End With
End Sub


JLGWhiz

Adding to a ListBox
 
You can specify the index number to add an item in a certain position within
a listbox. However, there are two methods, depending on where you created
your listbox from. I suggest you review the VBA help file on AddItem method
for each type listbox.

"Pflugs" wrote:

I have a userform that contains a listbox. Users can add values by entering
number in a textbox and clicking an "add" button. The problem is once a
certain number of entries have been added, the next values appear at the end
of the listbox.

Using the .AddItem method, how do I add values to the beginning of the list?
The code keeps saying it expects an "=" sign.

Code:
Private Sub cbCustomCodeAdd_Click()
With Me
If Not .tbCustomCode.Value = "" Then
For Each item In .lbGathCodes.List
If item = .tbCustomCode.Value Then
.tbCustomCode.Value = ""
Exit Sub
End If
Next item
If Len(.tbCustomCode) 3 Then .tbCustomCode =
Left(.tbCustomCode, 3)
.lbGathCodes.AddItem (.tbCustomCode.Value)
.tbCustomCode.Value = ""
.lbGathCodes.Selected(.lbGathCodes.ListCount - 1) = True
End If
End With
End Sub


Pflugs

Adding to a ListBox
 
I looked through the help files, and for Userforms the syntax is:
Variant = object.AddItem [item,[varIndex]]

Without the "variant = " part, I can make the code work as below. However,
when I specify an index number, I get an error message saying "Expected =".
Then, when I declare a variant 'test' and set it equal to .ListBox.AddItem
(item, #), the compiler highlights the AddItem part and says "Expected
Function."

I don't know what it expects of me! Any further ideas?

Thanks, Pflugs

"JLGWhiz" wrote:

You can specify the index number to add an item in a certain position within
a listbox. However, there are two methods, depending on where you created
your listbox from. I suggest you review the VBA help file on AddItem method
for each type listbox.

"Pflugs" wrote:

I have a userform that contains a listbox. Users can add values by entering
number in a textbox and clicking an "add" button. The problem is once a
certain number of entries have been added, the next values appear at the end
of the listbox.

Using the .AddItem method, how do I add values to the beginning of the list?
The code keeps saying it expects an "=" sign.

Code:
Private Sub cbCustomCodeAdd_Click()
With Me
If Not .tbCustomCode.Value = "" Then
For Each item In .lbGathCodes.List
If item = .tbCustomCode.Value Then
.tbCustomCode.Value = ""
Exit Sub
End If
Next item
If Len(.tbCustomCode) 3 Then .tbCustomCode =
Left(.tbCustomCode, 3)
.lbGathCodes.AddItem (.tbCustomCode.Value)
.tbCustomCode.Value = ""
.lbGathCodes.Selected(.lbGathCodes.ListCount - 1) = True
End If
End With
End Sub



All times are GMT +1. The time now is 05:17 PM.

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