Excel VBA: Looping through Userform selected Listbox Values to Worksheet Cell

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Code:
Private Sub cmdPrint_Click()
'' --------------------------------------------------------------------------------
'' Printing
'' --------------------------------------------------------------------------------
Dim i As Integer
Dim lItem As Long
Dim wsh As Worksheet
Dim ans As Boolean
    
    ans = Application.Dialogs(xlDialogPrinterSetup).Show
    
    If ans Then
      
        i = tbxCopies.Value
        
        Call Shell("rundll32 printui.dll,PrintUIEntry /e /n" & Application.ActivePrinter & ")")
        
        MsgBox i & " copies will be printed.", vbInformation, "Info"
        
        For lItem = 0 To lboRisks.ListCount - 1
            If lboRisks.Selected(lItem) = True Then
                Set wsh = ThisWorkbook.Worksheets("BRP Dashboard")
                wsh.Range("R4").Value = lboRisks.Value
                wsh.PrintOut Copies:=i
                Set wsh = Nothing
            End If
        Next
    
    Cells(5, 11).Activate
    Unload Me
     
End Sub

Lets try that again, my post seem to have not posted correctly.

I have the above code where I am trying to loop through my selected listbox values and send in turn to cell R2 on my dashboard, instead I seem to be sending "True" to cell R2 instead

Can anyone help identify my error?

Thx in advance
 
Upvote 0
Code:
With lboRisks
            For lItem = 0 To .ListCount - 1
                If .Selected(lItem) Then
                    Set wsh = ThisWorkbook.Worksheets("BRP Dashboard")
                    wsh.Range("R4").Value = .List(lItem)
                    wsh.PrintOut Copies:=i
                    Set wsh = Nothing
                End If
            Next lItem
End With

Solution found!
 
Upvote 0
Since the sheet being referenced doesn't change, you can assign it to your variable prior to your With/End With statement...

Code:
Set wsh = ThisWorkbook.Worksheets("BRP Dashboard")

With lboRisks
    For lItem = 0 To .ListCount - 1
        If .Selected(lItem) Then
            wsh.Range("R4").Value = .List(lItem)
            wsh.PrintOut Copies:=i
        End If
    Next lItem
End With


Set wsh = Nothing
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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