Results 1 to 3 of 3

Thread: ListBox Control [Multiple Selection]

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    1

    ListBox Control [Multiple Selection]



    I'm using Excel VBA to do this. But this sub seems to copy everything from sheet 1 to sheet 2? Please advise. I want only multiple selected items in ListBox to be copy. Does anyone know good VB site for beginner? ListBox guide? MSDN too deep for me.

    Thanks for all!


    VB Code:
    1. Sub GenSelectedSQL()
    2.     Dim rngColC As Range
    3.     Dim strFirstAddress As String
    4.     Dim rngNextResult As Range
    5.     Dim rngNextSearch As Range
    6.     Dim i As Long
    7.     Dim c As Long
    8.      
    9.     Set rngColC = Sheets("Sheet1").Range("C:C").CurrentRegion
    10.      
    11.     With UserForm1.ListBox1
    12.     For i = 0 To .ListCount - 1  ' first listbox index is 0!
    13.     Set rngNextResult = rngColC.Find(What:=ListBox1.List(i, 0), LookIn:=xlValues, LookAt:=xlWhole, _
    14.     SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    15.     If Not rngNextResult Is Nothing Then
    16.      'copy row and paste it somewhere
    17.     rngNextResult.EntireRow.Copy Destination:=Worksheets("Sheet2").Cells(rngNextResult.Row, 1)
    18.     End If
    19.      'now if there's a match, find any others
    20.     If Not rngNextResult Is Nothing Then
    21.     Do  'change the search range to the remainder of the range
    22.     Set rngNextSearch = Range(rngNextResult, rngColC.End(xlDown))
    23.     Set rngNextResult = rngNextSearch.FindNext(rngNextResult)
    24.     strFirstAddress = rngNextResult.Address
    25.     If Not rngNextResult Is Nothing Then
    26.     rngNextResult.EntireRow.Copy Destination:=Worksheets("Sheet2").Cells(rngNextResult.Row, 1)
    27.     End If
    28.     Loop While Not rngNextResult Is Nothing And rngNextResult.Address <> strFirstAddress
    29.     End If
    30.     Next i
    31.     End With  
    32. End Sub

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ListBox Control [Multiple Selection]

    Welcome to the Forums sheeeng.

    Moved to VBA from ClassicVB

    Here are a couple of links that I found for you from Google.

    http://www.mindspring.com/~tflynn/excelvba.html
    http://www.excel-vba.com/

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: ListBox Control [Multiple Selection]

    You're iterating through all the items in the listbox and copying them to sheet 2 if they're found in the selected range but you're not actually checking whether they were selected in the list box. Change the if statement:-
    VB Code:
    1. If Not rngNextResult Is Nothing Then
    to:-
    VB Code:
    1. If Not rngNextResult Is Nothing and .selected(i) Then
    and it should only copy the selected ones.

    Actually that's just the easiest fix to explain - ideally you should probably check .selected(i) immediately inside the for loop to save yourself the time and trouble of selecting the range etc when the items not even selected.

    ...and indent

Posting Permissions

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



Click Here to Expand Forum to Full Width