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
 

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,808
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)
 

N_J_C

New Member
Joined
Aug 7, 2014
Messages
4
My listbox isn't on a form, it's just on a worksheet. I'll try and check the name of it though. Thanks
 

N_J_C

New Member
Joined
Aug 7, 2014
Messages
4
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
 

N_J_C

New Member
Joined
Aug 7, 2014
Messages
4
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
 

Forum statistics

Threads
1,082,258
Messages
5,364,088
Members
400,779
Latest member
lumers

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top