Thread: Listbox help
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson[_2_] Chip Pearson[_2_] is offline
external usenet poster
 
Posts: 95
Default Listbox help

Todd,

To automatically select the values in the list box that appear in Z1:Z26,
use the following code:

Private Sub UserForm_Initialize()
Dim Rng As Range
Dim Ndx As Long
'
' select values in Z1:Z26
'
With Me.ListBox1
For Each Rng In Range("Z1:Z26")
For Ndx = 0 To .ListCount - 1
If Rng.Text = .List(Ndx) Then
.Selected(Ndx) = True
Exit For
End If
Next Ndx
Next Rng
End With
End Sub

To append the selected values at the end of the existing entries in column
Z, uses

Private Sub CommandButton1_Click()
Dim Rng As Range
Dim Ndx As Long
Set Rng = Cells(Rows.Count, "Z").End(xlUp)(2, 1)
With Me.ListBox1
For Ndx = 0 To .ListCount - 1
If .Selected(Ndx) = True Then
Rng.Value = .List(Ndx)
Set Rng = Rng(2, 1)
End If
Next Ndx
End With
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Todd Huttenstine" wrote in message
...
Dim Rng As Range
Dim Ndx As Long
Set Rng = Range("Z1")
With Me.ListBox1
For Ndx = 0 To .ListCount - 1
If .Selected(Ndx) = True Then
Rng.Value = .List(Ndx)
Set Rng = Rng(2, 1)
End If
Next Ndx
End With



Above is the code I am using... It populates with the
values found in Range Z1:Z26.

Is there a way to when I pull up the
listbox, that it can look in that range(Z1:Z26) and if it
finds a value that matches one of the list box values, it
can put a check next to it automatically in the listbox.
For instance, lets say I open the form and put a check
mark next to "Todd", then I close the form and later on
reopen it, I would like to see the check mark already next
to "Todd" in the listbox when I pull up the userform.

Also lets say later on I open the form
and try to check more values in it. If I do that now, it
just overwrites whats currently in the range. I want it
to be that when I check the checkboxes and then click ok
button, that the code not does not overwrite values that
are in the range.

Thank you

Todd Huttenstine