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]
 

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
 

Forum statistics

Threads
1,082,250
Messages
5,364,033
Members
400,774
Latest member
Goldi paul

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top