在一页中使用范围选择单元格导出为pdf

时间:2018-05-25 08:08:36

标签: excel vba excel-vba

我无法以横向格式导出到PDF,我的所有范围选择,我在哪里做错了?

Sub Macro()
Dim wksht As Worksheet
Set wksht = ActiveSheet

Dim path As String
path = "C:\test\"

If Len(Dir(path, vbDirectory)) = 0 Then
    MkDir path
End If

Dim rngeStart
Dim rngeEnd

Set rngeStart = wksht.UsedRange.Find(What:="####", LookIn:=xlValues, LookAt:=xlWhole)
Set rngeEnd = wksht.UsedRange.FindNext(After:=rngeStart)

Dim dataRange As Range
Set dataRange = wksht.Range(rngeStart, rngeEnd)

Dim wb As Workbook
Dim i As Long

For i = 1 To wksht.Range("A" & wksht.Rows.Count).End(xlUp).Row
  wksht.Copy
  With ActiveSheet
     .Range(.Cells(1, 1), .Cells(1, rngeStart.Column - 1)).EntireColumn.Delete
     .Rows("1:" & rngeStart.Row - 1).Delete
End With

此部分用于PDF格式导出,只有一页没有选择所有范围:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & wksht.Range("A" & i).Value & ".pdf", _ Quality:=xlQualityStandard,
    IncludeDocProperties:=True, IgnorePrintAreas:=False 
ActiveWorkbook.Close 
  Next i
End Sub

pdf中是否有“适合一页”功能?

1 个答案:

答案 0 :(得分:3)

您是否尝试在pdf导出之前调整活动表格式? 请在子宏()的开头添加此代码:

'START FORMAT THE ACTIVE SHEET
Application.PrintCommunication = False

With ActiveSheet.PageSetup
    .CenterHorizontally = True
    .CenterVertically = True
    .Orientation = xlLandscape 'xlPortrait
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
End With

Application.PrintCommunication = True
'END FORMAT THE ACTIVE SHEET