Loop through selected Listbox items problem

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to loop through the selected items in a multi-select Listbox on a userform;

Code:
    With Me.ListBox1        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                
                Application.ScreenUpdating = False
                Sheet2.Activate
                Sheet2.Visible = xlSheetVisible
                Sheet2.Range("D10").Value = .List(i, 0) & " " & .List(i, 1)
                Sheet2.Range("D11").Value = "Unit"
                Sheet2.Range("D12").Value = .List(i, 2)
                
                'Requal course
                If ComboType = "Requalification" Then
                    CourseDate = Format(TextDate1, "dd mmm yyyy")
                    Sheet2.Range("D15").Value = "REQUALIFICATION"
                    Sheet2.Range("D16").Value = "OPERATOR"
                    Sheet2.Range("D18").Value = Format(TextDate1, "dd mmmm yyyy")
                    Sheet2.Range("D20").Value = ComboInst1.Text
                    Sheet2.Range("D23").Value = ComboInst2.Text
                    FName = "Authority - " & Sheet2.Range("D10").Value & ".pdf"
                    path = ThisWorkBook.path & "\Courses\Requal Courses\" & CourseDate & "\ " & FName
                    Sheet2.ExportAsFixedFormat Type:=xlTypePDF, FileName:=path, IgnorePrintAreas:=False, openafterpublish:=False
                End If
                
                'Initial course
                If ComboType = "Initial" Then
                    CourseDate = Format(TextDate1, "dd mmm yyyy")
                    Sheet2.Range("D15").Value = "INITIAL"
                    Sheet2.Range("D16").Value = "OPERATOR"
                    Sheet2.Range("D18").Value = Format(TextDate2, "dd mmmm yyyy")
                    Sheet2.Range("D20").Value = ComboInst1.Text
                    Sheet2.Range("D23").Value = ComboInst2.Text
                    FName = "Authority - " & Sheet2.Range("D10").Value & ".pdf"
                    path = ThisWorkBook.path & "\Courses\Initial Courses\" & CourseDate & "\ " & FName
                    Sheet2.ExportAsFixedFormat Type:=xlTypePDF, FileName:=path, IgnorePrintAreas:=False, openafterpublish:=False
                End If
                
                
            End If
        Next i
        
    End With

What I need it to do is create a pdf file for each selected item - it does the first one it finds but not any after that. Can someone please show me where I am going wrong?
 
I think I understand - so the code is looping through all of the Listbox items and checking if they are selected, rather than doing what I thought it did which was loop through only the selected ones.

That doesn't explain though why it works if I select 1 item but doesn't if I select multiple.
 
Last edited:
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Do the cells on sheet2 get updated for each selected item in the listbox?
 
Upvote 0
Not if I select more than 1 item in the Listbox - they do if I only select a single item.
 
Upvote 0
How are you running that code, is it from a command button?
 
Upvote 0
In that case I don't know what is going wrong, as it work fine for me.
 
Upvote 0
Thanks Fluff, I think I know what the issue is, but not why - I've put a breakpoint in just after screenupdating here;

Code:
            End If        Next i
        
    End With
    
    Application.ScreenUpdating = True
    Sheet2.Visible = xlSheetHidden
    Sheet3.Activate

When I go back to the form there are no items selected in the Listbox - surely that shouldn't be the case?

I have nothing that triggers on the Listbox change event so don't understand why this is happening. Thanks so far!
 
Upvote 0
What happens if you remove this line from the top of your code
Code:
Sheet2.Activate
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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