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