Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3545|回复: 1

vba 打印pdf在一张表上

[复制链接]
发表于 2019-6-8 22:20 | 显示全部楼层 |阅读模式
2学分
程序主要是改变一个工作表的格式然后转换成pdf,蓝色部分是工作表格式,黄色部分是工作簿的设置和pdf打印,我想的是将pdf调整到一张表上而不是分开的。
图片是正确的格式和错误的格式。

运行时候第一次是正确的格式,之后就是错误的格式。

Sub daqinqin()
    Dim n As Integer, i As Integer, fn
    Dim r As Integer  '行数
    Dim filename, Wb As Workbook, sht As Worksheet

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.PrintCommunication = False

    fn = Dir(ThisWorkbook.Path & "\病历*.xlsx")
    Do While fn <> ""
        myfile = ThisWorkbook.Path & "\" & fn
        Set Wb = GetObject(myfile)

        With Wb.Worksheets(1)
            r = Wb.Worksheets(1).[A65536].End(3).Row
            .Range("A1:BC" & r).HorizontalAlignment = xlCenter '水平居中


            .Range("A1:BC" & r).VerticalAlignment = xlCenter  '垂直居中
            .Range("A1:BC" & r).WrapText = True  '自动换行


            .Columns("A:A").ColumnWidth = 24
            .Columns("B:O").ColumnWidth = 13
            .Columns("P:P").ColumnWidth = 100
            .Columns("Q:T").ColumnWidth = 10
            .Rows("1:" & r).RowHeight = 366
        End With

        '格式化
        For Each sht In Wb.Worksheets
            With sht.PageSetup
                .Orientation = xlLandscape
                .Zoom = False
                .FitToPagesWide = 1
                .FitToPagesTall = 1
            End With
        Next

       Wb.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
        ThisWorkbook.Path & "\" & Left(fn, Len(myfile) - 5) & ".pdf"
        Wb.Windows(fn).Visible = True
        Wb.Close
       Set Wb = Nothing
        fn = Dir
    Loop


    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.PrintCommunication = True
End Sub


正确的格式.PNG
错误的格式.PNG

求助.zip

28.85 KB, 下载次数: 3

举例

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2019-7-4 00:25 | 显示全部楼层
Application.PrintCommunication = False这句话多余的去掉就好。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-7 10:28 , Processed in 0.186690 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表