2016-11-14 11 views
0

ExcelでVBAマクロをセットアップして、同じアカウント番号の行セットごとにPDFを出力しようとしています。私は同様の目的で見つかったマクロからこれを調整しています。私は2つのシート、データとアカウントを持っています。データシートには、行ヘッダーとしてフィルタリングされていないデータ[A0、MTIME、MDATE、MINIT、MTEXT]があり、アカウントシートにはプルしたいユニークなアカウント番号があります。Excel VBAマクロを使用して列の値に基づいて複数のPDFを出力する

フィルタリングは正常に機能しているように見えますが、マクロは最初の出力コンポーネントで死に至りましたが、なぜ私は少しぼんやりしています。確認されたアクセス許可は良いですか。どんな考えも評価されるだろう。以下のコード。

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 
+1

私はそれを試して動作します。あなたが死ぬことはどういう意味ですか?エラーダイアログ?エラーメッセージとは何ですか? – z32a7ul

+0

Set UniqueRngで始まる行は、最後の行の数値をA1:A20に連結するため、コードが非常に非効率になるため、最後の行が100の場合は20100個の項目を反復処理します。あなたのインデントを改善してください、それを読むのは難しいです。 – z32a7ul

+0

アカウントシートのセルには何が含まれていますか?彼らのキャラクターはファイル名で合法ですか? – z32a7ul

答えて

0

z32a7ul私が修正しなければならなかったいくつかの項目で正しいトラックに入れてください。究極の問題は、モジュールレベルではなくシートレベルでマクロを構築したことでした。コードをモジュールに移動し、それを元に戻して編集しました。ありがとうz32a7ul!

関連する問題