How to add month to an export macro name? - excel

i have an macro that exports worksheets to pdf's to a designated folder using their sheet name. What I would like is to add the month in as part of the filename. I can retrieve this from a cell in my workbook. Where in my code would I add this reference to?
Dim outFldr As String
Dim ws As Worksheet
Dim i As Variant, sheets_to_select As Variant
outFldr = ActiveWorkbook.Path
sheets_to_select = Array("Summary", "PLC", "MI", "Venture", "EIS", "VCT", "PE", "Debt")
For Each i In sheets_to_select
ThisWorkbook.Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=outFldr & "\" & i & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next i
MsgBox ("All pdf's exported.")
End Sub

Read the date from Excel, use for example format to get the month number or name and add that to the file name. I prefer to use intermediate variables as this makes the code much easier to read and debug.
Adapt the format to create the pattern you want - you could also include the year.
For Each i In sheets_to_select
Dim filename As String, filedate As Date, month As String
filedate = ThisWorkbook.Sheets(1).Range("A1") ' Change this so that it reads your cell with the date
month = Format(filedate, "mm") ' For month as 2-digits
month = Format(filedate, "mmm") ' For month as 3 letters month name
month = Format(filedate, "mmmm") ' For month as full month name
filename = outFldr & "\" & i & "_" & month & "_.pdf"
ThisWorkbook.Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, _
filename:=filename, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next i

Related

Modify created macro to print array of sheets instead of Activesheet

I have a Macro that i have managed to put together (its rough, and im new to VBA but it does what i want - for the most part) It currently prints the active sheet to PDF and names it based on cell values. I want to adapt this to print 2 sheets into a single file (if its separate files, thats more than ok!) The cell Value naming bit can be changed at the top which i can do, but its calling for the export to pdf bit that im having an issue with.
I have tried reading up on the Activeworkbook functions but im not having much luck. I have tried calling for a sheet array, but it doesnt like the exportasfixedformat Type:= and im kind of new to that part too. It likes it in the original code, but not when i try and change the ActiveWorkbook.ActiveSheet, it spits it.
It would finalise my calculator :) Any help would be greatly appreciated.
Code:
Sub GetFilePath_Click()
Dim FileAndLocation As Variant
Dim strFilename As String
strFilename = Sheets("Leave Loading").Range("F13") & ", " & Sheets("Leave Loading").Range("F12") & " - " & Sheets("Leave Loading").Range("F14") & "- " & "Leave Loading" & ".pdf"
FileAndLocation = Application.GetSaveAsFilename _
(InitialFileName:=strPathLocation & strFilename, _
filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select a Location to Save")
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFilename, OpenAfterPublish:=True
End Sub
Thank you in advance!
Option Explicit
Sub GetFilePath_Click()
Dim FileAndLocation As Variant
Dim strFilename As String, strPathLocation As String
strPathLocation = ""
With Sheets("Leave Loading")
strFilename = .Range("F13") & ", " & .Range("F12") & " - " _
& .Range("F14") & "- Leave Loading" & ".pdf"
End With
FileAndLocation = Application.GetSaveAsFilename _
(InitialFileName:=strPathLocation & strFilename, _
filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select a Location to Save")
Sheets(Array("Sheet2", "Sheet4")).Select
Sheets("Sheet2").Activate
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFilename, OpenAfterPublish:=True
End Sub

VBA Excel - Export PDF file from Excel with second page

Just want to ask if how can I export a PDF file with vba? The thing is I do have a 10-F and 10-B sheet. The code below is working in the 10-F sheet. My problem is how can I export the data in the 10-B sheet together with the 10-F? The first page is the data in 10-F while the data in 10-B will be on the second page.
The range for the 10-B sheet is "B10:AD92".
Sub Ver_PDF()
'Create and assign variables
Dim saveLocation As String
Dim rng As Range
lname = ThisWorkbook.Sheets("HOME").Range("K12")
fname = ThisWorkbook.Sheets("HOME").Range("K13")
Name = fname & " " & lname
pdfile = "V-" & Name & ".pdf"
saveLocation = ThisWorkbook.Path & "\V-PDF\" & pdfile
Set rng = Sheets("10-F").Range("B9:AD89")
Dim strFileExists As String
strFileExists = Dir(saveLocation)
If strFileExists <> "" Then
Dim Ret
'~~> Change this to the relevant file path and name
Ret = IsFileOpen(saveLocation)
If Ret = True Then
MsgBox "Please close the PDF file before proceeding...", vbCritical + vbOKOnly, "Error"
Exit Sub
End If
End If
rng.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=saveLocation, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Any help is highly appreciated! Thanks!
This code will do what you want. Change the worksheet names in the array as well as the destination path and file name.
Sub ExportAsPDF()
Dim FolderPath As String
Dim FileName As String
FolderPath = "D:\Test PDFs\" ' change to suit: end on back-slash
FileName = "Test" ' change to suit
On Error Resume Next
MkDir FolderPath
On Error GoTo 0
Worksheets(Array("10-F", "10-B")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=FolderPath & FileName & ".PDF", _
OpenAfterPublish:=True, _
IgnorePrintAreas:=False
MsgBox "PDF was successfully created."
Worksheets(1).Select
End Sub
Change OpenAfterPublish to False if you don't want to see the result right away.

How to insert multiple values in a single excel cell and export it as a pdf for every entry of that cell

i want to insert multiple values in a single cell and export every single entry as a separate pdf. The only thing i did till know is to manually reference the cells and export them as pdfs. This is my macro:
Sub SavePDF()
Range("A8").Value = Range("A8").Value + 1
Sheet3.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Users\Report_" & _
ActiveSheet.Range("A8").Value & ".pdf", _
OpenAfterPublish:=False
End Sub
Lets say that i have a range: M6:M14 and i want to input the results in the cell "M1". After i start the macro i want ot create for every single value (the value should be inside the pdf) a new pdf. Example: for the value of M6 a pdf, for M7 another and so on till i reach M14.
Please try this code.
Sub SavePDF()
Dim NameRange As Range
Dim i As Integer
Dim PdfName As String
Set NameRange = Range("M6:M14")
For i = 1 To NameRange.Cells.Count
PdfName = Trim(Range("A8").Value) & i
With Sheet3
.Range("M1").Value = NameRange.Cells(i).Value
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\Users\Report_" & PdfName & ".pdf", _
OpenAfterPublish:=False
End With
Next i
End Sub

VBA exits before end

EDIT
I got it to run thru about 1,000 part numbers, but now it stops and excel freezes. Is it possibly too much for excel to handle? Would it be better to add a filter and have user perform this task based off criteria xyz?
I'm using the following code to create a mass form replacement. It's intended to run through a list of 5000+ part numbers, paste the part number into a designated range on another worksheet where it creates the form and saves it as a pdf in a specified folder. It works up until row 105 and then stops. It does what it's supposed to, other than continue down the sheet. I have the same code (modified slightly) being used on another sheet and it runs perfect. I'm not sure why its stopping after a certain number of rows
Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
Dim x As Worksheet
Dim y As Worksheet
Dim i As Long
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To FinalRow
Set x = ThisWorkbook.Worksheets("Part Number Database")
Set y = ThisWorkbook.Worksheets("BOM")
x.Cells(i, 1).Copy Destination:=y.Range("E3:J3")
strFile = Range("E3") & ".pdf"
strPathFile = strPath & strFile
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\AGoodwin\Desktop\BOM\" & strFile & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Next i
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End Sub

save pdf as cell name with multiple cells

I am trying to save a pdf with the name being info in a cell, but the name involves multiple cells. the cells have formulas inputted because they pull from a seperate sheet in my spreadsheet. how would i could about doing this? this is the formulas in the cells that i want to pull the name for the pdf from:
=VLOOKUP('Work Order'!F26,'Fort McMurray File Services'!A2:AJ1515,6,FALSE)
=VLOOKUP('Work Order'!F26,'Fort McMurray File Services'!A2:AJ1515,6,FALSE)
And this is the code i am using in vba:
Private Sub filename_cellvalue()
'Update 20141112
Dim Path As String
Dim filename As String
Path = "C:\Users\meghan lewis\Desktop\MASS DEMO"
filename = .Range("C7").Value & .Range("D7").Value & _
.Range("E7").Value & .Range("F7").Value & _
.Range("G7").Value & .Range("H7").Value & .Range("I7").Value
ActiveWorkbook.SaveAs filename:=Path & filename & ".PDF", FileFormat:=xlNormal
End Sub
Sub SaveAsPDF()
Dim fName As String
With Worksheets("WORK ORDER")
fName = .Range("C7").Value & .Range("D7").Value & _
.Range("E7").Value & .Range("F7").Value & _
.Range("G7").Value & .Range("H7").Value & _
.Range("I7").Value
End With
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
"C:\Users\meghan lewis\Desktop\MASS DEMO & fName", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
When I try to save it saves the pdf as mass demo rather than pulling from the cells. Please advise.
How does your code even compile? You have unqualified range addresses like .Range("C7").Value, .Range("D7").Value etc in Sub filename_cellvalue
In Sub SaveAsPDF(), change "C:\Users\meghan lewis\Desktop\MASS DEMO & fName" to "C:\Users\meghan lewis\Desktop\MASS DEMO" & fName
fname within quotes will behave as a String and not a variable.

Resources