PDA

View Full Version : Solved: Add ListBox selections to specific cell range



cgannm
12-18-2008, 10:37 AM
Hi, can you help modify code to add ListBox selections in range B5:B15 only.
Also, I would like for ListBox to append a new selection within this range. This code puts new selections starting in B5 and overwrites selections already made.

Thanks,
cgannm



Private Sub CommandButton2_Click()
Dim lItem As Long
Dim cell As Range
Set cell = ActiveSheet.Range("B5")
For lItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lItem) = True Then
cell.Value = ListBox1.List(lItem)
Set cell = cell.Offset(1, 0)

ListBox1.Selected(lItem) = False
End If
Next
End Sub

mdmackillop
12-18-2008, 01:30 PM
Is this what you're after?
Private Sub CommandButton2_Click()
Dim lItem As Long
Dim Rng As Range
Set Rng = ActiveSheet.Range("B5:B15")
i = 1
For lItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lItem) = True Then
Do
If Rng(i) = "" Then
Rng(i).Value = ListBox1.List(lItem)
Exit Do
End If
i = i + 1
Loop Until i > Rng.Cells.Count
ListBox1.Selected(lItem) = False
End If
Next

End Sub

Kenneth Hobs
12-18-2008, 01:35 PM
The 2nd line is to insure that your input will be put in B5 if no data occurs from B5 and down.

Set cell = ActiveSheet.Range("B" & Rows.Count).End(xlUp)
If cell.Row < 5 Then Set cell = Range("B5")

cgannm
12-22-2008, 12:14 PM
Thanks! This work great.

Your time and effort are very much appreciated.

Happy Holiday