Single ListBox Loop help needed

N_J_C

New Member
Joined
Aug 7, 2014
Messages
4
Hi

I have a worksheet with a single listbox. I need to loop through all the values in the listbox and make a PDF of the sheet. (The listbox items change all the values on the worksheet). I've searched and found some code that should work, but it isn't and I have no idea what is wrong.

This is my code. It is stopping at:
Code:
For i = 0 To ListBox3.ListCount - 1
.

Code:
Sub ExportToPDF()

Dim filename As String
Dim i As Integer

For i = 0 To ListBox3.ListCount - 1
If ListBox3.Selected(i) Then
        filename = Range("A13").Value
        Sheets("Working Results Sheet").ExportAsFixedFormat xlTypePDF, "filepath is here" & filename & ".pdf"
End If
Next i

End Sub

Any help would be massively appreciated
Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Are you sure the listbox is called: ListBox3 ?
Thats the only thing I can see , could be wrong.
(unless your code is NOT in the form w the listbox)
 
Upvote 0
My listbox isn't on a form, it's just on a worksheet. I'll try and check the name of it though. Thanks
 
Upvote 0
Ok, so I changed my listbox name just to be sure, but it is still not working. Does anyone have any ideas?

Is it to do with where my list box is? It's on the top of a worksheet. I tried selecting that worksheet on this code, but it still didn't work.

Here is my code now:
Code:
Sub ExportToPDF()
Dim filename As String
Dim i As Integer

For i = 0 To SchoolSelectBox.ListCount - 1
If SchoolSelectBox.Selected(i) Then
        filename = Range("A13").Value
        Sheets("Working Results Sheet").ExportAsFixedFormat xlTypePDF, "[I]filepath is here[/I]" & filename & "Results Sheet" & ".pdf"
End If
Next i

End Sub

It is still stopping at:
Code:
For i = 0 To SchoolSelectBox.ListCount - 1
 
Upvote 0
Ok, I've managed to get some sort of result. This code now saves the PDF, but only for the selected item in the listbox. The loop is not working. I want it to select the first item, PDF it then select the next one, PDF it, and so on until the end of the list.

Can anyone tell me where I'm going wrong?

Code:
Sub ExportPDF3()
 
  Dim i As Integer
  Dim filename As String
  
  For i = 0 To ActiveSheet.Shapes("SchoolSelectBox").ControlFormat.ListCount - 1
    filename = Range("A13").Value
    Sheets("Working Results Sheet").ExportAsFixedFormat xlTypePDF, "[I]filepath is here[/I]" & filename & "Results Sheet" & ".pdf"
  Next i
 
End Sub

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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