如何使用用户窗体中的列表框将多个工作表导出为PDF

时间:2019-04-26 00:28:53

标签: excel vba userform export-to-pdf

我正在尝试将多个工作表导出到一个PDF文档中,从而允许用户通过用户表单选择哪些工作表。我已经写了下面的代码,但是一直卡在“ Sheets(SheetArray())。Select”行中,我无法弄清楚。有人有什么想法吗?

Private Sub CommandButton1_Click()

Dim SheetArray() As Variant
Dim indx As Integer

Dim ws As Worksheet
Dim strPath As String
Dim myfile As Variant
Dim strFile As String
Dim sheetstoprint As String

Set ws = ActiveSheet

strFile = Worksheets("Setup").Range("G8").Text & " Proforma" & ".pdf"

strFile = ThisWorkbook.Path & "\" & strFile

myfile = Application.GetSaveAsFilename _
    (InitialFileName:=strFile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and FileName to save")

If myfile <> "False" Then

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    indx = 0
    For i = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) = True Then
                ReDim Preserve SheetArray(indx)
                SheetArray(indx) = Sheets(ListBox1.List(i, 1)).Index
                indx = indx + 1
            End If
    Next i

    If indx > 0 Then

            Sheets(SheetArray()).Select
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                Filename:=myfile, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=True


                '.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


    End If
End If
exitHandler:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub


Resume exitHandler
End Sub

Private Sub ListBox1_Click()

End Sub

Private Sub UserForm_Initialize()

Dim wks() As Variant


wks = Array("Cover Page", "Proforma (1)", "Proforma (2)", "Proforma (3)", "Proforma (4)", "Expense Analysis", "Assumptions", _
                        "Payroll Schedule", "Expense Comp")

'Debug.Print wks(16)
For i = 0 To UBound(wks)

        ListBox1.AddItem wks(i)
        ListBox1.List(ListBox1.ListCount - 1, 1) = wks(i)

Next i


End Sub

希望从用户表单中获取选定的工作表,以正确导出为单个PDF。

0 个答案:

没有答案
相关问题