【问题标题】:Convert Excel Sheet to PDFs, Infinite Loop Error将 Excel 工作表转换为 PDF,无限循环错误
【发布时间】:2021-10-28 21:25:13
【问题描述】:

我在论坛上使用了一些代码来创建一个宏,该宏可以从 Excel 中的单个工作表中导出 PDF。每个 PDF 都包含与员工相关的标题和所有相关行(所有带有员工 ID 的行)。当我运行它时一切顺利并且 pdf 是正确的,但是宏永远不会停止运行。

我相信我创建了一个无限循环,不知道如何纠正它。

它还会创建一个仅包含不需要的标题的 PDF。

Sub PracticeToPDF()

Dim ws As Worksheet
Dim ws_unique As Worksheet
Dim DataRange As Range
Dim iLastRow As Long
Dim iLastRow_unique As Long
Dim UniqueRng As Range
Dim Cell As Range
Dim LastRow As Long
Dim LastColumn As Long

Application.ScreenUpdating = False

'Note that the macro will save the pdf files in this active directory so you should save in an appropriate folder
DirectoryLocation = ActiveWorkbook.Path

Set ws = Worksheets("BootVSPayroll") 'Amend to reflect the sheet you wish to work with
Set ws_unique = Worksheets("BootVSPayroll") 'Amend to reflect the sheet you wish to work with

'Find the last row in each worksheet
iLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
iLastRow_unique = ws_unique.Cells(Rows.Count, "A").End(xlUp).Row


With ws
    'I've set my range to reflect my headers which are fixed for this report
    Set DataRange = ws.Range("$A$1:$K$1" & iLastRow)

    'autofilter field is 4 as I want to print based on the practice value in column D
    DataRange.AutoFilter Field:=1

    Set UniqueRng = ws_unique.Range("A1:A20" & iLastRow_unique)
    For Each Cell In UniqueRng
        DataRange.AutoFilter Field:=1, Criteria1:=Cell

    Name = DirectoryLocation & "\" & Cell.Value & " BOOT Report" & ".pdf"

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

    Next Cell

End With
With ws
     .Protect Userinterfaceonly:=True, _
     DrawingObjects:=False, Contents:=True, Scenarios:= _
    True, AllowFormattingColumns:=True, AllowFormattingRows:=True
     .EnableOutlining = True
     .EnableAutoFilter = True
     If .FilterMode Then
        .ShowAllData
     End If
 End With
Application.ScreenUpdating = True

End Sub

【问题讨论】:

    标签: excel vba pdf-generation infinite-loop


    【解决方案1】:

    您的意思是:

    Set DataRange = ws.Range("$A$1:$K$1" & iLastRow)
    ...
    Set UniqueRng = ws_unique.Range("A1:A20" & iLastRow_unique)
    

    改为:

    Set DataRange = ws.Range("$A$1:$K$" & iLastRow)
    ...
    Set UniqueRng = ws_unique.Range("A1:A" & iLastRow_unique)
    

    ?

    例如,如果(取自 ws_unique)您最后使用的行,iLastRow_unique 等于 200,那么 "A1:A20" & iLastRow_unique 等于 "A1:A20200"——这可能是您打算循环遍历的更多行,我想。

    【讨论】:

    • 你的巫师,谢谢乔治。我现在唯一需要修复的是它会创建一个额外的 pdf,其中只包含不必要的标题。
    • 我不确切知道工作表是什么样的,所以我只是在这里吐口水,但我会试一试。 “额外”的 PDF 是它生成的第一个吗?如果是这样,您可能希望这些范围中的一个或两个从一行或一列开始。我的猜测是 UniqueRng 可能需要从 A2 而不是 A1 开始,但这又是一种盲目的猜测,没有看到工作表。相反,如果它是最后一个生成的 PDF,则可能是在最后一个“真实”之后的行中存在空格或其他一些意外内容,并且它正在创建一个 PDF。
    猜你喜欢
    • 2017-01-31
    • 1970-01-01
    • 2021-09-19
    • 2020-10-10
    • 2021-08-20
    • 2021-11-20
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多