在打印到PDF时,导出工作表中过滤的行并排除指定的列

时间:2019-06-09 20:31:03

标签: excel vba

我有一个宏,可以将一张纸导出为PDF。

Sub RentalEquipmentList()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = Sheets("RENTALS")
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\")) & 
"23-Portal Traveler\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for savng file
strName = ThisWorkbook.Sheets("General Info").Range("B8").Value _
      & " Rental Equipment List "
'create default name for savng file
strFile = strName & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected   
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
 'confirmation message with file info
 MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub

我导出了整张纸。

我需要根据“ A”列的值过滤出行。

我还要如何从导出的范围中删除某些列?
示例:我不需要在导出的文件中看到第3,4列?

1 个答案:

答案 0 :(得分:1)

首先过滤A列,然后导出为PDF,然后清除过滤器...

If myFile <> "False" Then
    With wsA
        If .FilterMode Then .ShowAllData
        .UsedRange.AutoFilter Field:=1, Criteria1:="<>"
        .ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=myFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
        .UsedRange.AutoFilter
    End With
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
     & vbCrLf _
     & myFile
End If

编辑:在打印为PDF时要排除特定的列

If myFile <> "False" Then
    With wsA
        If .FilterMode Then .ShowAllData
        With .UsedRange
            .AutoFilter Field:=1, Criteria1:="<>"
            .Range("C:D").EntireColumn.Hidden = True
        End With
        .ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=myFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
        With .UsedRange
            .Range("C:D").EntireColumn.Hidden = False
            .AutoFilter
        End With
    End With
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
     & vbCrLf _
     & myFile
End If

希望这会有所帮助!