Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default 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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding item in listbox volabos Excel Worksheet Functions 0 December 3rd 07 11:47 AM
Adding item in listbox volabos Excel Worksheet Functions 0 December 3rd 07 11:27 AM
Adding Headers to a listbox Mike Milligan Excel Programming 3 October 18th 06 08:48 PM
adding a listbox to a spreadsheet [email protected] Excel Programming 2 October 29th 03 05:45 PM
Adding a row of data to a Listbox Scott Elrod[_2_] Excel Programming 0 August 27th 03 09:43 PM


All times are GMT +1. The time now is 01:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"