【问题标题】:Excel VBA Select worksheets from a list where the value is TRUEExcel VBA 从值为 TRUE 的列表中选择工作表
【发布时间】:2018-05-04 16:04:27
【问题描述】:

我正在尝试编写一个将一些表格打印为 pdf 的宏。我有一个工作表(“PDF”),在 D 列中列出工作簿中的所有工作表,在 F 列旁边我有一个 TRUE/FALSE 值列表。我的宏应该将 D 列中 F 列中为 TRUE 的所有工作表打印到单个 pdf 文件中。

此工作簿中的工作表数量确实有所不同。

下面是我第一次尝试这个代码

Sub PDFCreater()

Dim pdfName As String
Dim FullName As String
Dim myArray() As Variant
Dim ArrSize As Integer
Dim ArrWkst As String
Dim RowCnt As Long, ArrCnt As Long

pdfName = Sheets("PDF").Range("D1").Text
FullName = ThisWorkbook.Path & "\" & pdfName & ".pdf"

ReDim myArray(Sheets("PDF").Range("D2").Value)    'Size of array/ number of sheets in PDF
ArrCnt = 0
For RowCnt = 8 To 302
    If Sheets("PDF").Cells(RowCnt, 6).Value = True Then
        myArray(ArrCnt) = Cells(RowCnt, 4).Value
        ArrCnt = ArrCnt + 1
    End If
    RowCnt = RowCnt + 1
Next
'Select all worksheets in MyArray()
Sheets(myArray).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FullName _
, Quality:=xlQualityMedium, IncludeDocProperties:=False,
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    根据您的描述,您的代码中有几个错误

    Sub Test()
    Dim pdfName As String
    Dim FullName As String
    Dim myArray() As Variant
    Dim ArrSize As Integer
    Dim ArrWkst As String
    Dim RowCnt As Long, ArrCnt As Long
    
        pdfName = Sheets("PDF").Range("D1").Text
        FullName = ThisWorkbook.Path & "\" & pdfName & ".pdf"
    
        ' You need to re-dim the array in the loop in order to have an array with
        ' the correct dimension. Otherwisae the array is too big and will contain
        ' empty entries
        'ReDim myArray(Sheets("PDF").Range("D2").Value)    'Size of array/ number of sheets in PDF
        ArrCnt = 0
        For RowCnt = 8 To 302
            If Sheets("PDF").Cells(RowCnt, 6).Value Then
                ReDim Preserve myArray(ArrCnt)
                myArray(ArrCnt) = Cells(RowCnt, 4).Value
                ArrCnt = ArrCnt + 1
            End If
            ' the for loop will increase rowcnt itself
            ' no need to do that
            'RowCnt = RowCnt + 1
        Next
        'Select all worksheets in MyArray()
        Sheets(myArray).Select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FullName, _
                                        IncludeDocProperties:=False, _
                                        IgnorePrintAreas:=False, OpenAfterPublish:=True
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      使用循环隐藏具有 FALSE 值的工作表,然后:

      ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FullName _
              , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
              :=False, OpenAfterPublish:=True
      

      这会将整个工作簿保存为 PDF,隐藏的工作表除外。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-28
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多