0
我正在尝试在Excel中设置一个VBA宏,该宏使用相同的帐号为每组行输出一个PDF。我从一个为类似目的而发现的宏裁剪它。我有两张纸,数据和帐户。数据表包含未经过滤的数据[A0,MTIME,MDATE,MINIT,MTEXT]作为行标题,账户表中只有我想要提取的唯一账号。根据列值输出多个PDF的Excel VBA宏
该过滤似乎正常工作,但宏在第一个输出组件死亡,我有点难以理解为什么。已验证的权限是好的。任何想法都会被apprecaited。代码如下。
Sub PracticeToPDF()
'Prepared by Dr Moxie
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("Data") 'Amend to reflect the sheet you wish to work with
Set ws_unique = Worksheets("Account") '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:$E$" & 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 & " Account Notes" & ".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
我试了一下,它的工作原理。死亡是什么意思?错误对话框?什么是错误信息? – z32a7ul
以Set UniqueRng开头的行使代码非常低效,因为您将最后一行的数字连接到A1:A20,所以如果最后一行是100,那么您将迭代20100个项目。删除20.并改善你的缩进,很难阅读它。 – z32a7ul
账户表单元包含什么内容?他们的字符在文件名中合法吗? – z32a7ul