Yes, it is possible. Simply change the property value List
in the index of the selected object. Here is an example of code with two Userforms (one called "Test", which displays the Listbox, and another called "Edit" which displays the contents of the currently selected item from the list and lets you edit it):
Userform "Testing"
Screen image:
(Listbox is called "Listbox"; Userform "Test" is created once and reused)
Code:
Dim oEditForm As Editar
Private Sub ListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim sName As String
sName = Me.ListBox.List(Me.ListBox.ListIndex)
oEditForm.EditName.Text = sName
oEditForm.Show
If oEditForm.Tag = True Then
sName = oEditForm.EditName.Text
Me.ListBox.List(Me.ListBox.ListIndex) = sName
End If
End Sub
Private Sub UserForm_Initialize()
Set oEditForm = New Editar
Me.ListBox.AddItem "Tyrion Lannister"
Me.ListBox.AddItem "Jaime Lannister"
Me.ListBox.AddItem "Cersei Lannister"
Me.ListBox.AddItem "Daenerys Targaryen"
Me.ListBox.AddItem "Jon Snow"
Me.ListBox.AddItem "Petyr Baelish"
Me.ListBox.AddItem "Jorah Mormont"
End Sub
Userform "Edit"
Screen image:
(the buttons are called "Ok" and "Cancel", and the Textbox is called "Editname")
Code:
Private Sub Cancel_Click()
Me.Tag = False
Hide
End Sub
Private Sub Ok_Click()
Me.Tag = True
Hide
End Sub
Upshot
Screen image:
Explanation of the code:
- First you need to capture the appropriate double click event, called
*_DblClick
.
- Then, get the text of the item currently selected in the list from
ListBox.List
passing his index which is in ListBox.ListIndex
.
- Put this value in your other edit form and allow the user to edit it. When they click Ok, go to the next step.
- Having the user edited the value, update the content of the same item currently selected in the list (that is, using
ListBox.List
passing his index which is in ListBox.ListIndex
).
Hello. It’s not clear what the problem is. Would you like to put in the list the values typed by the user in the Textboxes, is that it? Another thing: why the hell you make a loop is if you take items one by one?
– Luiz Vieira
I am trying to edit data by Lixtbox, so in the double click opens Userform and the textboxes are loaded with the values of the selected Lixtbox line. I’m not getting these values changed after editing. How much the loop I thought would solve, was the last attempt I made. I’m a layman on the subject.
– ludehenrique2cia
Got it. It’s really not at all clear on the question. How about adding some screenshots of your running project? Another thing: you mention "spreadsheet", but by the way just want to know how to update the value of a line in a Listbox (and not in the spreadsheet! and so he got an answer that didn’t help you).
– Luiz Vieira
I responded on the basis as far as I understand than you described in comment. But I will keep my vote to close until you edit the question and make it clear there, ok?
– Luiz Vieira
Luiz good morning! You reported another item that had not paid attention that is save the change in the spreadsheet as well. Because when doing a research in the future will already be arranged. But saving in the spreadsheet is very difficult for me to adjust the code I believe.
– ludehenrique2cia
Dude, I’m sorry, but I don’t understand what you’re saying. " Saving in the spreadsheet is very difficult for me (sic) to adjust the code" -> this was a comment or a question?
– Luiz Vieira
It was a comment. I’m not getting missing the "New Edit". I can’t attach the screen print!
– ludehenrique2cia
If you have another question, you should open a new question. Read [Ask]. ;)
– Luiz Vieira