Export Excel Workbook in .xls Format - vba

I have an Excel that gets updated with sales numbers daily. At the end of the week, when the Excel is complete, I export a PDF copy of the WEEKLY worksheet. Once I have a PDF copy, the sales numbers are transferred to another sheet within the workbook, emptying the WEEKLY worksheet.
In addition to this PDF copy of the WEEKLY worksheet, I'd like to export the entire workbook in a separate Excel file to the same location (.xls format is fine). I'd like to do this before emptying the WEEKLY worksheet. I've tried using a save as macro, but I want to remain in my original Excel - not the newly saved file.
For reference, here's the VBA code for my PDF export:
Sub SaveWeekly()
'
' SaveWeekly Macro
'
'
Sheets("WEEKLY").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Z:\Excel New\Previous Excels\" & Range("A1") & " " & Range("H1") & ", " & Format(Date, "yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Any help is appreciated.

Try this code
Sub Test()
Dim strDate As String
Dim strTime As String
strDate = Format(Date, "DD-MM-YYYY")
strTime = Format(Time, "hh.mm.ss")
Application.DisplayAlerts = False
With ActiveWorkbook
.SaveCopyAs fileName:=ThisWorkbook.Path & "\" & strDate & "_" & strTime & "_" & .Name
End With
Application.DisplayAlerts = True
End Sub

Related

Save as PDF and Excel 2016 Copy In Folder and Filename created from Cell Value

I'm working with Excel 2016, I have created a Template with a main Sheet(G-Card) and the following 3 sheets get the information from the first sheet.
I have a Macro that allows me to save all 4 sheets into PDF creating a Folder in a specific location and saving it under that folder using cell values. I have also manage to save the workbook as a copy under the same filename in the same folder but it says that it is not compatible or extension error.
The only extension that seems to work is .xlm. I need to be able to save the template as a PDF and as an Excel file in the same folder including the macro in the 2016 version. Help please.. here is my code:
Sub NewFolder_SaveasPDF_CLICK()
Dim strFilename As String
Dim FldrName As String
On Error Resume Next
Set rngRANGE = Worksheets("G-Card").Range("U3")
FldrName = Worksheets("G-Card").Range("O4").Value
strFilename = rngRANGE.Value & " "
MkDir "Q:\Green Cards & Acknowledgement\Green Cards & Acknowledgement\2017 Orders\" & FldrName
Sheets(Array("G-Card", "P-AKG", "W-AKG", "Y-AKG")).Select Sheets("G-Card").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"Q:\Green Cards & Acknowledgement\Green Cards & Acknowledgement\2017 Orders\" & FldrName & "\" & strFilename & Worksheets("G-Card").Range("O4").Value & ".pdf" _
, QUALITY:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True _
Dim ExcelWork As String
ExcelWork = strFilename & FldrName
ActiveWorkbook.SaveAs FileName:="Q:\Green Cards & Acknowledgement\Green Cards & Acknowledgement\2017 Orders\" & FldrName & "\" & ExcelWork & ".xlm", _
FileFormat:=xlNormal, Password:="", _
WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
End Sub

Save multiple worksheets as PDF

I'm fairly green with using VBA and this is the first time I've posted a question in this forum so bear with me if I'm breaking any posting rules.
Below is a piece of code I use to print/save 1 or more worksheets from an Excel workbook to PDF. It works fine if the data from each worksheet that I'm exporting fits onto a single page. The problem I'm finding is that if the range of data from my first sheet is larger than 1 page then only the 1st worksheet will be exported, and only up to the first page-break. Any advice or suggestions on how I can resolve this issue?
'Ask user if report needs to be saved as a pdf
PdfCheck = MsgBox("Save report as PDF", vbYesNo, "Save PDF")
'use result of <PdfCheck> to control if statement
If PdfCheck = vbYes Then
Pdfsheets = InputBox("How many worksheets would you like to include in PDF")
'Prints the number of sheets entered by user
'starts at first ACTIVE Worksheet to the result of <Pdfsheets>
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"C:\Users\" & WinName & "\Documents\REPORTING\" & Database & "\" & _
ReportYear & "\" & FolderMonth & "\" & Database & "-" & Title & "-" & FileDate _
, Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, From:=1, To:=Pdfsheets, OpenAfterPublish:=True
Else
GoTo X
End If
X:
End Sub
The method I use, is to loop through the worksheets within the workbook and select the ones that meet my criteria. Then I use ExportAsFixedFormat to create my pdf.
Note you cannot select hidden worksheets, so if the workbook contains them you will need to add an if/then to check visibility.
Pdfsheets = InputBox("How many worksheets would you like to include in PDF")
for x=1 to Pdfsheets
thisworkbook.sheets(x).select (false) 'use false to select multiple sheets
next x
'Prints the number of sheets entered by user--starts at first ACTIVE
Worksheet to the result of <Pdfsheets>
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"C:\Users\" & WinName & "\Documents\REPORTING\" & Database & "\" & ReportYear & "\" & FolderMonth & "\" & Database & "-" & Title & "-" & FileDate _
, Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True

exporting a certain worksheet from a workbook to pdf

i am an amateur programmer learning how to program using vba
without further a due, my question is :
i have created a listbox (listbox1) where i've listed all my sheets(ws) name in a specific workbook.
i've also created another listbox(listbox2) where when i select some sheets name on the listbox1, they will be transfer to listbox2.
my main objective is that by choosing one or more sheets listed in the listbox2, and by clicking a button, i'll manage to save all the selected sheets in one pdf file.
here is the code for the button used to export the file in pdf that i've written but i've only managed to export them not in one pdf file but in numerous amount of pdf file.
Dim NomTableau() As String
For Each WkbkName In application.Workbooks()
If WkbkName.Name = choix_poteau.Value & "_" & section & "_" & projet & ".xlsx" Then
WkbkName.Activate
GoTo lois
End If
Next
Set wbk = Workbooks.Open(add1 & "\" & Me.projet.Value & "\" & Me.section.Value & "\poteaux\" & Me.choix_poteau.Value & "_" & Me.section & "_" & Me.projet & ".xlsx")
lois:
For i = 0 To ListBox2.ListCount - 1
While ListBox2.List(i) <> ""
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name = ListBox2.List(i) Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Export\Resultats__" & ListBox2.List(i - counter) & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End If
Next
Wend
Next i
End Sub
thanks alot, really appreciate your help
Instead of exporting each sheet individually, select them all first and then call the ExportAsFixedFormat method.
Here is my test code, which worked as expected:
Option Explicit
Private Sub TestPDF()
Dim i As Integer
Dim arrSheets() As String
Dim strSheets As String
'Get our sheet names
For i = 1 To 3
strSheets = Worksheets(i).Name & "," & strSheets
Next
'Trim the trailing comma
strSheets = Left(strSheets, Len(strSheets) - 1)
arrSheets = Split(strSheets, ",")
ThisWorkbook.Sheets(arrSheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\qzbcjs\Documents\Useful Workbooks\test.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
I declared an array of strings, populated a string variable with the comma-delimited names of the worksheets (in my case I just wanted the first through third sheets), split that comma-delimited string into an array and used the array to select all of the desired sheets and only called the ExportAsFixedFormat method once.
Adapting this method to your code, beginning as the lois part would look something like this:
lois:
Dim ws As Worksheet
Dim arrSheets() As String
Dim strWs As String
For i = 0 To ListBox2.ListCount - 1
While ListBox2.List(i) <> ""
For Each ws In Worksheets
If ws.Name = ListBox2.List(i) Then
strWs = ws.Name & "," & strWs
End If
Next
Wend
Next i
strWs = Left(strWs, Len(strWs) - 1)
arrSheets = Split(strWs, ",")
ThisWorkbook.Sheets(arrSheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Export\Resultats__" & ListBox2.List(i - counter) & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True

VBScript to export selected portion of Excel sheet to a PDF, on one page

I currently have a VBScript that exports an entire excel worksheet to a PDF. I was wondering if it would be possible to set the script to only export the selected areas. I know this can be done manually, but I've been tasked with automating this process. If achieved, it would make the whole reporting proccess a lot easier. The VBScript that I'm currently using is:
Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Set ws = ActiveSheet
'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
& "_" _
& Format(Now(), "yyyymmdd\_hhmm") _
& ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "PDF version of report has been created."
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
I've already tried replacing the "Set ws = ActiveSheet" to "Selection", and that has yielded no results. Also, as a side note, when selections are sent to a PDF, they appear on separate sheets.Is it possible to put them both on one sheet?
Thanks in advance for any help that you can offer me.
Try setting the Print Area that you want to output to PDF.

How can I SaveAs a new workbook to original workbook directory with Excel VBA?

Basically, I'd like to save some worksheets into separate new workbooks in the same system location as original notebook I am deriving from.
I recognize that the default path is to save something new is to the location of the current notebook, but perhaps since I am opening a new workbook the default reverts to the user's Document's folder, which is where they are saving right now.
I "learned" VBA over the last couple of days, so advice on other things you notice is cool too, but the saveas is what's bothering me.
Dim ws As Worksheet
Dim wb As Workbook
Dim dept_array As Variant
Dim dept As Variant
' Add or remove a department name spelled exactly as it is in the filter
dept_array = Array("HR", "IT", "Marketing", "Product Marketing", "Sales", "Channels", "Presales", "Direct", "Sales Ops", "R&D", "Support", "G&A")
Application.ScreenUpdating = False
For Each ws In Workbooks("Weekly Department Transaction Report.xlsm").Worksheets
For Each dept In dept_array
If Application.Proper(ws.Name) = Application.Proper(dept) _
Then
Set wb = Workbooks.Add
ThisWorkbook.Sheets(dept).Copy Before:=wb.Sheets(1)
wb.Saveas dept & "_" & Format(Now, "yyyymmdd") & ".xlsx"
Workbooks("Weekly Department Transaction Report.xlsm").Sheets("Codes").Copy After:=Workbooks(dept & "_" & Format(Now, "yyyymmdd") & ".xlsx").Sheets(dept)
Workbooks("Weekly Department Transaction Report.xlsm").Sheets("How").Copy Before:=Workbooks(dept & "_" & Format(Now, "yyyymmdd") & ".xlsx").Sheets(dept)
Workbooks(dept & "_" & Format(Now, "yyyymmdd") & ".xlsx").Save
End If
Next dept
Next ws
Application.ScreenUpdating = True
End Sub
Please let me know if I am not following the correct stackoverflow format.
Longtime user first time asker :)
Edit this line in your code:
wb.SaveAs FileName:= ThisWorkbook.Path & "\" & dept & "_" & Format(Now, "yyyymmdd") & ".xlsx"