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?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hello,

Just remove

Code:
[COLOR=#333333]Application.ScreenUpdating = False[/COLOR]

Hope this will help
 
Upvote 0
Hi, and thanks - I've done that but it makes no difference and it still only creates 1 record where I select multiple.
 
Upvote 0
Hello,

You could have your Export instruction
Code:
[COLOR=#333333]Sheet2.ExportAsFixedFormat Type:=xlTypePDF, FileName:=path, IgnorePrintAreas:=False, openafterpublish:=False[/COLOR]
moved just after the End If line ...

HTH
 
Upvote 0
If you step through the code using F8 does the value in D10 change?
 
Upvote 0
Hi Fluff - yes, it does indeed.....

However - this only occurs if I select 1 item from the Listbox, if I select more than one it steps from this line;

Code:
If .Selected(i) Then

straight to this line;

Code:
End If

(the one immediately before 'Next i').....
 
Last edited:
Upvote 0
That means you didn't select that particular value in the listbox
 
Upvote 0
I'm confused, which isn't difficult - I thought the code would loop through each selected item?

I haven't specified to only run if a particular item is selected in the Listbox, (though have for a Combobox but can't see how that would make a difference as it's evaluating that part correctly).
 
Upvote 0
Your code will loop through all the items in the ListBox & check if they have been selected.
If an item is not selected, then the code will skip to the End If and then check the next item in the ListBox.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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