简体   繁体   中英

Using VBA to print to PDF existing macro

So i found this code online and was able to edit it to do what i want EXCEPT save as a PDF it currently set to only show me a print preview. Can someone explain how to edit this to save as a PDF with the File name being what ends up appearing in cell "A2"

Sub testme()

Dim TempWks As Worksheet
Dim wks As Worksheet

Dim myRng As Range
Dim myCell As Range

'change to match your worksheet name
Set wks = Worksheets("Sheet3")
Set TempWks = Worksheets.Add

wks.AutoFilterMode = False 'remove the arrows

'assumes headers only in row 1
wks.Columns(1).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=TempWks.Range("A1"), Unique:=True

With TempWks
    Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With

With wks
    For Each myCell In myRng.Cells
        .UsedRange.AutoFilter Field:=1, Criteria1:=myCell.Value
        Dim MyFileName As Variant
Dim MyfilePath As Variant
Dim rng As Range
Set wks = Worksheets("Sheet3")
Set rng = wks.Cells(2, 1)

MyfilePath = "C:\Users\mmunoz\Desktop\Teresa" 'this is whatever location you wish to save in

MyFileName = MyfilePath & "\" & rng.Value & ".pdf" 'You can do the below in just a couple of lines, but this is way more effective and stops issues later on

    ChDir _
    MyfilePath ' hold your save location


wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    MyFileName, Quality:= _
     xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False 'did you want to open the file after saving?
    Next myCell
End With

Application.DisplayAlerts = False
TempWks.Delete
Application.DisplayAlerts = True

End Sub

I have a bunch of data that I need to filter to show only a client's lines of data and save that as a PDF to send to the client.

Thanks,

This is the gist of what you want. I've added comments to explain

Dim MyFileName As Variant
Dim MyfilePath As Variant
Dim rng As Range

Set rng = wks.Cells(2, 1)

MyfilePath = "N:\Desktop" 'this is whatever location you wish to save in

MyFileName = MyfilePath & "\" & rng.Value & ".pdf" 'You can do the below in just a couple of lines, but this is way more effective and stops issues later on

    ChDir _
    MyfilePath ' hold your save location


 wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
   MyFileName, Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True 'did you want to open the file after saving?

Option Explicit Sub testme()

Dim TempWks As Worksheet
Dim wks As Worksheet

Dim myRng As Range
Dim myCell As Range

'change to match your worksheet name
Set wks = Worksheets("Sheet3")

Set TempWks = Worksheets.Add

wks.AutoFilterMode = False 'remove the arrows

'assumes headers only in row 1
wks.Columns(1).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=TempWks.Range("A1"), Unique:=True

With TempWks
    Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With

With wks
    For Each myCell In myRng.Cells
        .UsedRange.AutoFilter Field:=1, Criteria1:=myCell.Value
Dim MyFileName As Variant
Dim MyfilePath As Variant
Dim rng As Range

Set rng = wks.Cells(2, 1)

MyfilePath = "C:\Users\mmunoz\Desktop\Teresa" 'File Location

MyFileName = MyfilePath & "\" & myCell.Value & ".pdf" 'File Name

    ChDir _
    MyfilePath


wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  MyFileName, Quality:= _
     xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
  OpenAfterPublish:=False
    Next myCell
End With

Application.DisplayAlerts = False
TempWks.Delete
Application.DisplayAlerts = True

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM