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

GedSalter

New Member
Joined
Apr 24, 2019
Messages
25
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]
 

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
25
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
 

Forum statistics

Threads
1,081,538
Messages
5,359,388
Members
400,526
Latest member
Brook1083

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top