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

GedSalter

Board Regular
Joined
Apr 24, 2019
Messages
80
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]
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top