Edit an Item in a Listbox

SlinkRN

Well-known Member
Joined
Oct 29, 2002
Messages
715
Hi all, I can't seem to figure this one out by searching. It seems to be such a simple thing! I have a text box that fills a listbox with team names. After the user fills in a bunch of team names, I would like them to be able to click on one of the names in the listbox and edit it. I've tried this code but it is an infinite loop. Can anyone help me figure this out?
<code>
Private Sub lstTeams_Click()
Dim i As Integer
Dim sel As String
Dim chg As String
For i = 0 To ListBox1.ListCount - 1
If lstTeams.Selected(i) Then
sel = lstTeams.List(i)
chg = InputBox("Edit Item here", "Edit Name")
lstTeams.List(i) = chg
End If
Next i

End Sub
</code>
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Whenever you change the value, it triggers the event again. Try
Code:
[COLOR=#ff0000]Dim DisableEvents As Boolean[/COLOR]
Private Sub ListBox1_Click()
   Dim i As Integer
   Dim sel As String
   Dim chg As String
   
   If DisableEvents Then Exit Sub
   DisableEvents = True
   For i = 0 To ListBox1.ListCount - 1
      If ListBox1.Selected(i) Then
         sel = ListBox1.List(i)
         chg = InputBox("Edit Item here", "Edit Name")
         ListBox1.List(i) = chg
      End If
   Next i
   DisableEvents = False
End Sub
The line in red must go at the very top of the module, before any code.
Change the Listbox name to suit
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
You're welcome & thanks for the feedback

After playing, I'm going to have to put in some sort of error check for when the user clicks "Cancel" on the input box. When I did that it erased the listbox item rather than leaving it as is.
 
Upvote 0
You can use
Code:
         If chg <> "" Then ListBox1.List(i) = chg
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,245
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top