Consulting

Results 1 to 4 of 4

Thread: Solved: Add ListBox selections to specific cell range

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Posts
    18
    Location

    Solved: Add ListBox selections to specific cell range

    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


    [VBA]
    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
    [/VBA]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Is this what you're after?
    [VBA]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
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    The 2nd line is to insure that your input will be put in B5 if no data occurs from B5 and down.

    [vba]Set cell = ActiveSheet.Range("B" & Rows.Count).End(xlUp)
    If cell.Row < 5 Then Set cell = Range("B5")[/vba]
    Last edited by Kenneth Hobs; 12-18-2008 at 02:05 PM.

  4. #4
    VBAX Regular
    Joined
    Jun 2007
    Posts
    18
    Location
    Thanks! This work great.

    Your time and effort are very much appreciated.

    Happy Holiday

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •