在VBA中使用自动过滤的数据生成PDF

时间:2018-07-10 16:57:43

标签: excel vba excel-vba

我正在尝试将信息过滤到列表中,然后将其导出为PDF。下面的代码正确地提取了正在使用的已过滤行的数量,但是当数据转换为PDF时,它仅提取了几列。例如,如果LastRow = 70,则仅输出七行,因为那些是唯一的真实行值低于70的行。如果我忽略使用LastRow计数,由于原始数据集通常超过500,我将获得大约20张空白纸行长。有谁知道这个问题的好解决方案?我能想到的唯一解决方案是将单元格值复制到单独的工作表中,并在该工作表中PDF。如果可能的话,我想避免这种方法。

Private Sub CommandButton1_Click()

' GENERATE REPORTS
Dim LastRow As Integer
LastRow = Sheets("Target
List").AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1

MsgBox (LastRow)
If CheckBox1.Value = True Then
Dim intChoice As Integer
Dim strPath As String

intChoice = Application.FileDialog(msoFileDialogSaveAs).Show

If intChoice <> 0 Then

strPath = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)

Sheets("Renew Target List").PageSetup.Orientation = xlLandscape

Sheets("Renew Target List").Range("A1:AZ" & LastRow).ExportAsFixedFormat 
Type:=xlTypePDF, _
Filename:=strPath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Application.PrintCommunication = True
    End If
End If

End Sub

0 个答案:

没有答案