Saving multiple selected worksheets from a listbox as PDF

thochi

New Member
Joined
Nov 12, 2011
Messages
6
Hi there - reacquainting myself with VBA and I seem to be a bit rusty.

To this point I have been able to successfully write code that will save a constant set of worksheets as a pdf. However, I would now like to alter it to be able to dynamically select the desired worksheets from a list box (I have been able to populate my list box) and then save as a pdf. The last step is where I am have issues. This is what I have thus far..

Dim relativePath As String
Dim Selected As Long


For Selected = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(Selected) = True Then
Sheets("Summary").Range("Q65536").End(xlUp)(2, 1) = ListBox1.List(Selected)
ListBox1.Selected(Selected) = False
End If
Next


relativePath = ThisWorkbook.Path & "\" & Sheets("Title").Range("B28").Value
Sheets (Array(ListBox1.Selected(Selected)))
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=relativePath

End With

Any insight is appreciated!

Thanks,
Thomas
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Thomas

You'll need to construct an array of the selected sheets.
Code:
Dim arrSheets()
Dim relativePath As String
Dim idx As Long    ' don't use Selected, that's a listbox property.:)
Dim cnt As Long

    For idx = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(idx) Then
            ReDim Preserve arrSheets(cnt)
            arrSheets(cnt) = ListBox1.List(idx)
        End If
    Next idx

    If idx > 0 Then
        relativePath = ThisWorkbook.Path & "\" & Sheets("Title").Range("B28").Value
        Sheets(arrSheets).Select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=relativePath
    End If
 
Upvote 0
Does it select the sheets that where selected in the listbox?
 
Upvote 0
Oops, there's actually a couple of typos in the code.:oops:

Give this a go.
Code:
Dim arrSheets()
Dim relativePath As String
Dim idx As Long    ' don't use Selected, that's a listbox property.:)
Dim cnt As Long

    For idx = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(idx) Then
            ReDim Preserve arrSheets(cnt)
            arrSheets(cnt) = ListBox1.List(idx)
            cnt = cnt + 1
        End If
    Next idx

    If cnt > 0 Then
        relativePath = "C:\TEST\" & Sheets("Title").Range("B28").Value
        Sheets(arrSheets).Select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=relativePath
    End If
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,941
Members
449,480
Latest member
yesitisasport

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