I tried your solution and it works very well.Originally Posted by shrivallabha
I have change your code from -Set ws = Sheet("Data")- to -Set ws = ActiveSheet so it work with the active sheet independently of the sheet 's name.
however, your code is a "workaround" , I like to write the data directly in the list box but it seems a very difficult thing.
A question for you o for other member of the forum
Can you understand why this code does not write the values in 2° column of listbox1 (Lst1 in the code) but fill ok column 1 ???
[vba]Private Sub UserForm_Initialize()
Dim col As Collection, col2 As Collection
Dim v As Variant, v2 As Variant
Dim sht As Worksheet
Dim lng As Long
Set col = New Collection
Set col2 = New Collection
Set sht = ActiveSheet
On Error Resume Next
With ActiveSheet
For lng = 4 To 500
If Cells(lng, 6) <> "" Then
col.Add .Cells(lng, 6).Value, _
CStr(.Cells(lng, 6).Value)
If Cells(lng, 7) <> "" Then
col2.Add .Cells(lng, 7).Value, _
CStr(.Cells(lng, 7).Value)
End If
End If
Next
End With
With Me.Lst1
Lst1.Clear
For Each v In col
Lst1.AddItem v
Next
For Each v2 In col2
Lst1.Column(2).AddItem v2
Next
End With
Set col = Nothing
Set col2 = Nothing
Set sht = Nothing
End Sub
[/vba]
anyway, Many thanks for the solution that I will use from now until I can find a best way.