VBA to create new worksheet based on cell info and also save as a PDF file.

GedSalter

New Member
Joined
Apr 24, 2019
Messages
27
Hi all<

I have a vba which creates the new worksheet no problem. But I want to add to the VBA by having it also save as a pdf file in the following location:

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Ged\Disability\2019-2020\invoices.

I also want the PDF file to be named the same as the worksheet that was created.

This is what I have for that:

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub Add_Sheet()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Dim ws As Worksheet
Dim wh As Worksheet
Set ws = Worksheets(ActiveSheet.Name)
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
Set wh = Worksheets(Sheets.Count)
If ws.Range("e9").Value <> "" Then
wh.Name = ws.Range("E9").Value
End If
wh.Activate
Range("A1").Select[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] End Sub


This works great but when I try an add the save as PDF it wont do that side of it. Hope someone can help. I have never tried to have a VBA do two things before.

regards

Ged
[/FONT]
<strike>
</strike>
[/FONT]
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

joshman108

Active Member
Joined
Jul 6, 2016
Messages
310
Try this?

Code:
Sub Add_Sheet()Dim ws As Worksheet
Dim wh As Worksheet
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler






Set ws = Worksheets(ActiveSheet.Name)
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
Set wh = Worksheets(Sheets.Count)
If ws.Range("e9").Value <> "" Then
wh.Name = ws.Range("E9").Value
End If
wh.Activate
Range("A1").Select






strPathFile = "C:\Ged\Disability\2019-2020\invoices\" & wh.Name


' user can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")


'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If


exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub
I assume the location is in your C drive, but you can change it on this line if needed

Code:
strPathFile = "C:\Ged\Disability\2019-2020\invoices\" & wh.Name
 

GedSalter

New Member
Joined
Apr 24, 2019
Messages
27
Hi Josh.

I have been trying this. It creates a PDF but in "documents" folder only. I cant open the PDF because it says it doesnt exist. The file contains 0bytes
 

Watch MrExcel Video

Forum statistics

Threads
1,089,935
Messages
5,411,357
Members
403,362
Latest member
DoubleJay

This Week's Hot Topics

Top