Hi all ,
I'm good in Excel with Formulas and Pivot Tables but have like 0.5/10 understanding of VBA. I have studied some programming before so have a slight idea on syntax , operators etc.
99% of my job I don't need to use VBA but I'm working on something and need to in this case.
In Excel , I have a pricelist which we create a PDF version to send to customer. Up to now this was done manually (using the steps bel0w) but I want to add a button that does it in a single click. I was very lucky that I found some code online which works 99.9% perfectly for me. The 0.1% is that I would like to be able to create 2 pages per sheet rather than the default 1.
Manually this is done by File > Print > Printer (Select ADOBE PDF) > Printer Properties > Layout > Pages Per Sheet = 2
I've spent 2 hours googling on how I can add something to my code but it seems such a function does not exist. I've added the code I'm using below. I would be very grateful if someone can advise me on how I can write a code that will do the > Pages Per Sheet = 2.
I imagine it is just a single line of code to add to the below if such a thing is possible in VBA.
Many thanks!!!
Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & ""
'replace spaces and periods in sheet name
' strName = Replace(wsA.Name, " ", "")
strName = Range("R10")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use 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:=True
'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'm good in Excel with Formulas and Pivot Tables but have like 0.5/10 understanding of VBA. I have studied some programming before so have a slight idea on syntax , operators etc.
99% of my job I don't need to use VBA but I'm working on something and need to in this case.
In Excel , I have a pricelist which we create a PDF version to send to customer. Up to now this was done manually (using the steps bel0w) but I want to add a button that does it in a single click. I was very lucky that I found some code online which works 99.9% perfectly for me. The 0.1% is that I would like to be able to create 2 pages per sheet rather than the default 1.
Manually this is done by File > Print > Printer (Select ADOBE PDF) > Printer Properties > Layout > Pages Per Sheet = 2
I've spent 2 hours googling on how I can add something to my code but it seems such a function does not exist. I've added the code I'm using below. I would be very grateful if someone can advise me on how I can write a code that will do the > Pages Per Sheet = 2.
I imagine it is just a single line of code to add to the below if such a thing is possible in VBA.
Many thanks!!!
Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & ""
'replace spaces and periods in sheet name
' strName = Replace(wsA.Name, " ", "")
strName = Range("R10")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use 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:=True
'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