Listbox help

bphall

New Member
Joined
Mar 8, 2011
Messages
11
So I searched the forum here and found a bit of code from one of the members here that seems to help me get the items selected in my list box to my results sheet in excel. The only issue I'm now having is getting those selected items into the correct column on my sheet.

Any help would be greatly appreciated, below is the code I found here with my naming convention applied, I've also ignored one part of the code as it clear contents I need there.

I want to be able to get the code into column I2 & below if multiple selections are made.

Code:
i = i - 1
    With lstbxLoggingServiceType
    For x = 0 To .ListCount - 1
        If .Selected(x) Then
            i = i + 1
            ReDim Preserve varSelected(i)
            varSelected(i) = .List(x)
        End If
    Next x
    End With

    If i = -1 Then Exit Sub
    'Range("I:I").ClearContents
    Range(Cells(1, 2), Cells(UBound(varSelected) + 1, 2)) = _
    WorksheetFunction.Transpose(varSelected)
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Maybe try something like this...
Code:
Private Sub CommandButton1_Click()

    Dim x As Integer, i As Integer
    
    Application.ScreenUpdating = False

    ' Range("I2", Range("I" & Rows.Count).End(xlUp).Offset(1)).ClearContents
    i = 2   'Start row
    
    With lstbxLoggingServiceType
        For x = 0 To .ListCount - 1
            If .Selected(x) Then
                Range("I" & i).Value = .List(x)
                i = i + 1
            End If
        Next x
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Maybe try something like this...
Code:
Private Sub CommandButton1_Click()

    Dim x As Integer, i As Integer
    
    Application.ScreenUpdating = False

    ' Range("I2", Range("I" & Rows.Count).End(xlUp).Offset(1)).ClearContents
    i = 2   'Start row
    
    With lstbxLoggingServiceType
        For x = 0 To .ListCount - 1
            If .Selected(x) Then
                Range("I" & i).Value = .List(x)
                i = i + 1
            End If
        Next x
    End With
    
    Application.ScreenUpdating = True
    
End Sub


Man, you're awesome, that helped. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top