Copy listbox to another listbox vba
juliet
-
juliet -
juliet -
Hello,
I would like to copy the contents of one listbox to another listbox in a different userform using a command button.
Here is what I wrote, but it doesn't work. Is there something to change in the properties of the listbox? Because it works with textboxes.
Private Sub CommandButton1_Click()
Moyennehistorique.ListBox2.Value = Me.ListBox1.Value
End Sub
Thank you very much
Configuration: Windows XP / Internet Explorer 8.0
I would like to copy the contents of one listbox to another listbox in a different userform using a command button.
Here is what I wrote, but it doesn't work. Is there something to change in the properties of the listbox? Because it works with textboxes.
Private Sub CommandButton1_Click()
Moyennehistorique.ListBox2.Value = Me.ListBox1.Value
End Sub
Thank you very much
Configuration: Windows XP / Internet Explorer 8.0
8 réponses
The "Value" in a list box represents only the selected value.
Do you want to copy all the content (that is, the complete list)? Or just the selected value?
Do you want to copy all the content (that is, the complete list)? Or just the selected value?
It copies the content of listbox1 to listbox2:
Dim i As Integer
For i = 0 To ListBox1.ListCount - 1
ListBox2.AddItem ListBox1.List(i)
Next
Have fun
Dim i As Integer
For i = 0 To ListBox1.ListCount - 1
ListBox2.AddItem ListBox1.List(i)
Next
Have fun
Thank you, but it doesn't work, I wrote this:
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 0 To historiqueetm.ListBox1.ListCount - 1
Me.ListBox1.AddItem historiqueetm.ListBox1.List(i)
Next
End Sub
Because it's 2 different userforms.
Thank you very much.
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 0 To historiqueetm.ListBox1.ListCount - 1
Me.ListBox1.AddItem historiqueetm.ListBox1.List(i)
Next
End Sub
Because it's 2 different userforms.
Thank you very much.
It doesn't work = what?
an error message, or is it doing nothing?
otherwise, "Me." is not necessary. and how is historiqueetm loaded?
an error message, or is it doing nothing?
otherwise, "Me." is not necessary. and how is historiqueetm loaded?
I just removed the Me, but still nothing.
I don't have an error message, but nothing is happening.
I don't understand how loaded??
Thank you
I don't have an error message, but nothing is happening.
I don't understand how loaded??
Thank you
uh, well if it doesn't throw an error message, that means it works
but your source listbox doesn't contain any values, when I ask how it is loaded, I mean how is the content of the listbox populated?
all buttons, listboxes, comboboxes, textboxes, etc... are controls.
on an Excel userform, it needs to be initialized (loaded, populated) in order to read the content of this control.
the advantage of Excel is that once a userform is loaded, and its controls are initialized, you can close this userform and access the values from this userform in other userforms, but you must ensure that the closing procedure does not clear the controls, or that the control's data source is not dynamic and depends on other values.
add
Msgbox historiqueetm.ListBox1.ListCount
at the beginning of the button click
this will give you a popup with the number of rows visible in the listbox.
if it's 0 or -1, it means the listbox is not initialized.
you will need to provide more information on how you open your userforms, more code
but your source listbox doesn't contain any values, when I ask how it is loaded, I mean how is the content of the listbox populated?
all buttons, listboxes, comboboxes, textboxes, etc... are controls.
on an Excel userform, it needs to be initialized (loaded, populated) in order to read the content of this control.
the advantage of Excel is that once a userform is loaded, and its controls are initialized, you can close this userform and access the values from this userform in other userforms, but you must ensure that the closing procedure does not clear the controls, or that the control's data source is not dynamic and depends on other values.
add
Msgbox historiqueetm.ListBox1.ListCount
at the beginning of the button click
this will give you a popup with the number of rows visible in the listbox.
if it's 0 or -1, it means the listbox is not initialized.
you will need to provide more information on how you open your userforms, more code
Thank you for these details,
I added Msgbox historiqueetm.ListBox1.ListCount
and it showed 0.
I have a command button on a userform that opens 3 different userforms:
Private Sub CommandButton1_Click()
historiqueetm.Show
historiquegranulo.Show
historiquechimiques.Show
Dim i As Integer
MsgBox historiqueetm.ListBox1.ListCount
For i = 0 To historiqueetm.ListBox1.ListCount - 1
ListBox1.AddItem historiqueetm.ListBox1.List(i)
Next
End Sub
On these 3 userforms, there is a listbox that is populated like this:
Private Sub UserForm_activate()
TextBox1.Enabled = False
i = 4
If TextBox1.Value = "SIL9" Then
Do While Worksheets("analyses chimiques").Cells(3, i) <> ""
If IsNumeric(Worksheets("analyses chimiques").Cells(3, i).Value) Then
ListBox1.AddItem Worksheets("analyses chimiques").Cells(2, i).Value
End If
i = i + 1
Loop
End If
it repeats about 70 times and it works perfectly
End Sub
At first I didn't have 3 different userforms, but it directly wrote in my listboxes, but in the end it told me procedure too long that's why I ended up with 3 different userforms.
And actually I want the data from my 3 userforms to be in my 3 listboxes of the original userform and I would prefer if these 3 userforms did not appear.
I hope I was clear ...
Thank you for your understanding.
I added Msgbox historiqueetm.ListBox1.ListCount
and it showed 0.
I have a command button on a userform that opens 3 different userforms:
Private Sub CommandButton1_Click()
historiqueetm.Show
historiquegranulo.Show
historiquechimiques.Show
Dim i As Integer
MsgBox historiqueetm.ListBox1.ListCount
For i = 0 To historiqueetm.ListBox1.ListCount - 1
ListBox1.AddItem historiqueetm.ListBox1.List(i)
Next
End Sub
On these 3 userforms, there is a listbox that is populated like this:
Private Sub UserForm_activate()
TextBox1.Enabled = False
i = 4
If TextBox1.Value = "SIL9" Then
Do While Worksheets("analyses chimiques").Cells(3, i) <> ""
If IsNumeric(Worksheets("analyses chimiques").Cells(3, i).Value) Then
ListBox1.AddItem Worksheets("analyses chimiques").Cells(2, i).Value
End If
i = i + 1
Loop
End If
it repeats about 70 times and it works perfectly
End Sub
At first I didn't have 3 different userforms, but it directly wrote in my listboxes, but in the end it told me procedure too long that's why I ended up with 3 different userforms.
And actually I want the data from my 3 userforms to be in my 3 listboxes of the original userform and I would prefer if these 3 userforms did not appear.
I hope I was clear ...
Thank you for your understanding.