循环功能+保存PDF报告

时间:2018-05-20 12:50:21

标签: excel loops pdf

我正在使用循环功能来过滤数据并通过下拉菜单生成一堆报告。我想将每个报告的PDF保存为具有指定名称的特定文件位置。

我的代码中没有错误 - 但是目标文件位置中没有PDF。

有人可以帮助确定我可能出错的地方吗?

请原谅下面的代码 - 我的技能至多是基本的。

由于 HB

Sub PC_Report_Summary()

Dim PCreportreq As String
Dim PCfinalrow As Integer
Dim PClastprintrow As Integer
Dim PCclient As String
Dim PCbem As String
Dim PCreportlocation As String
Dim PCreportname As String
Dim i As Integer

Application.ScreenUpdating = False
Application.EnableEvents = False

Sheets("Summary").Range("B13:E1000").ClearContents
Sheets("PC Unit 8015").Range("P10:S1000").ClearContents

Sheets("PC Unit 8015").Select

PCreportreq = Sheets("PC Unit 8015").Range("B2").Value
PCfinalrow = Sheets("PC Unit 8015").Range("F1000").End(xlUp).Row
PClastprintrow = Sheets("PC Unit 8015").Range("P1000").End(xlUp).Row

For i = 10 To PCfinalrow
If Cells(i, 6) = PCreportreq Then
    Range(Cells(i, 2), Cells(i, 5)).Copy
    Range("P1000").End(xlUp).Offset(1, 0).PasteSpecial             xlPasteValuesAndNumberFormats
    End If
Next i

PClastprintrow = Sheets("PC Unit 8015").Range("P1000").End(xlUp).Row
PCclient = Sheets("Summary").Range("C8").Value
PCbem = Sheets("PC Unit 8015").Cells(i, 16).Value
PCreportlocation = Sheets("Summary").Range("C9").Value
PCreportname = PCreportlocation & " - " & PCclient & " - " & PCbem & ".pdf"

Sheets("PC Unit Test Report").Activate

For i = 10 To PClastprintrow
Sheets("PC Unit Test Report").Range("D11") = Sheets("PC Unit 8015").Cells(i, 16).Value

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

Next i

Sheets("PC Unit 8015").Range("P10:S1000").Copy
Sheets("Summary").Range("B1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats

End Sub

1 个答案:

答案 0 :(得分:0)

很难看出来自工作表的所有文件夹路径是否都设置正常,但这是一个有效的示例:

Sub saveAsPdf()

    Dim dir As String
        dir = "C:\Users\Evan\Desktop\"

    Dim sFileName As String
        sFileName = "helloWorld" & ActiveWorkbook.Name & "_" & Format(Date, "dd mmm")


    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        dir & sFileName & ".pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

End Sub

我想你错过了:

& Format(Date, "dd mmm")

修改

刚刚意识到,当您循环保存新PDF时,您不会更新

PClastprintrow = Sheets("PC Unit 8015").Range("P1000").End(xlUp).Row
PCclient = Sheets("Summary").Range("C8").Value
PCbem = Sheets("PC Unit 8015").Cells(i, 16).Value
PCreportlocation = Sheets("Summary").Range("C9").Value
PCreportname = PCreportlocation & " - " & PCclient & " - " & PCbem & ".pdf"

这些应该在保存pdf的第二个循环中,我假设路径名更改。它可能会保存相同的文件x10大声笑。