I recoreded a macro of me saving a file, but I want the name of the file to be
"OOR" & " " & Date & " " & Time
so the output should be OOR 10.18.2017 07:38 AM
Can someone help me with the code? Greatly apreciate it:
ChDir "C:\Users\spall1\Desktop\Base Business\Base Business Report\OOR Report"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\spall1\Desktop\Base Business\Base Business Report\OOR Report\OOR & DATE & TIME.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
You need to close the constant String part with " before adding the variable of Date and Time.
Change the last part of the string from:
\OOR & DATE & TIME.xlsx"
to:
\OOR" & Date & Time & ".xlsx"
Or, you could use VBA Now, which will give you both Date and Time:
\OOR" & Now & ".xlsx"
:
ActiveWorkbook.SaveAs _
Filename:= _
"C:\Users\spall1\Desktop\Base Business\Base Business Report\OOR Report\OOR " & Format(Now(), "YYYY-MM-DD HHMM") & ".xls" _
, FileFormat:=xlOpenXMLWorkbook _
, CreateBackup:=False
:
Related
I wrote a code for PDF export in Excel for Mac Office 2019 with a variable filename. It's working despite Mac's permission bugs with VBA saving as when using the makro for the first time Excel asks for the permission to access the concerning folder.
My question:
Can I anyhow give new users the option to choose their exporting path the first time they are using the makro or do I have to adjust the code for every new user personally?
Here's my (otherwise working) code so far:
Sub als_PDF_speichern_recorded_mac()
ChDir "/Users/Admin/Documents/Dokumente/Finanzen/2021/Rechnungen 2021/"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/Admin/Documents/Dokumente/Finanzen/2021/Rechnungen 2021/Rechnung_" & ActiveSheet.Range("D11") & ".pdf" _
, Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub
EDIT
I might have a solution working universally for Mac users. It's not following my original intent that users can choose the path at first use but it's supposed to be exporting the PDF to the current path of the workbook and creating a folder if not existing already.
Please could someone with Excel for Mac (I'm using 2019) confirm:
Sub exportPDF_mac()
'export to currentpath/greatnewfolder
ChDir "/" & ActiveWorkbook.Path
MakeFolderIfNotExist (ThisWorkbook.Path & Application.PathSeparator & "greatnewfolder")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/" & ActiveWorkbook.Path & "/greatnewfolder/greatfile_" & ActiveSheet.Range("D11") & ".pdf" _
, Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub
Function MakeFolderIfNotExist(Folderstring As String)
' Ron de Bruin, 2-March-2019
' http://www.rondebruin.nl/mac/mac010.htm
Dim ScriptToMakeFolder As String
Dim TestStr As String
Dim FolderStr As String
If Val(Application.Version) < 15 Then
ScriptToMakeFolder = "tell application " & Chr(34) & _
"Finder" & Chr(34) & Chr(13)
ScriptToMakeFolder = ScriptToMakeFolder & _
"do shell script ""mkdir -p "" & quoted form of posix path of (" & _
Chr(34) & Folderstring & Chr(34) & ")" & Chr(13)
ScriptToMakeFolder = ScriptToMakeFolder & "end tell"
On Error Resume Next
MacScript (ScriptToMakeFolder)
On Error GoTo 0
Else
FolderStr = MacScript("return POSIX path of (" & _
Chr(34) & Folderstring & Chr(34) & ")")
On Error Resume Next
TestStr = Dir(FolderStr & "*", vbDirectory)
On Error GoTo 0
If TestStr = vbNullString Then
MkDir FolderStr
End If
End If
End Function
Thanks in advance!
Thomas
I'm trying to help my mum remotely with her problem: she needs to save a workbook as an xlsx and a PDF. Here's my code:
Sub sb_Copy_Save_ActiveSheet_As_Workbook()
Dim wksht As Worksheet
Set wksht = ActiveSheet
Dim path As String
path = "C:\Users\" & Environ$("Username") & "\Company Name\Company Name Team Site - Documents\PO Numbers\"
wksht.Copy
ActiveWorkbook.SaveAs Filename:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".xlsx"
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".pdf" Quality:=xlQualityStandard OpenAfterPublish:=True
End Sub
We got it working to the point where she can save an xlsx file in the specified filepath, but attempting to export it as a PDF isn't working. She says she's getting a syntax error, but as I don't have excel myself I can't test it. I've looked at some similar questions but I can't seem to find an answer.
Thanks very much in advance
you just need to add commas so that
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".pdf" Quality:=xlQualityStandard OpenAfterPublish:=True
becomes
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".pdf", Quality:=xlQualityStandard, OpenAfterPublish:=True
i have this code for convering my excel file to pdf but after changing format, i have a pdf file with lots of pages(in every page just 4 coumns with 19 rows while my source file has 30 columns and rows , i want to see same as excel file but in pdf format , any body can help me! thanks in advance
Sub creatpdf()
Dim fName As String
Dim fname1 As String
With Worksheets("Output_" & Date)
fName2 = .Range("D3").Value
fName3 = "_BOM"
End With
BrowseForFolder = "X:\\output\\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
BrowseForFolder & "\" & fName2 & fName3 & "\" & "\" & fName2 & fName3 & "\" & "BOM" & "\" & fName2 & fName3, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
OpenAfterPublish:=False
End Sub
I have spent quite a bit looking for answer on the web, and maybe im just dumb?
any help would be greatly appreciated
FName = Environ("USERPROFILE") & "\Desktop" & "\" & IRN & Space(1) & OLDA & Space(1) & Format(Now, "mm-dd-yyyy") & Space(1) & ".pdf"
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName, _
Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=True, OpenAfterPublish:=False
There are two common reasons that would cause it to be unable to save the file.
The filename is invalid - this could occur in your case if either
of IRN or OLDA contained characters which are invalid in a file
name, such as a colon.
The file that is to be saved already exists but is open. This
would lock the file and prevent it being written over.
I tried to search and put together a code to fit my purpose.
Sub save()
ActiveWorkbook.SaveAS Filename:="C:\-docs\cmat\Desktop\New folder\ck.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End Sub
How to edit this to:
Instead of naming the saved file ck.xls, generate the filename from the worksheet cells C5 and C8, with a space in the middle.
try
Sub save()
ActiveWorkbook.SaveAS Filename:="C:\-docs\cmat\Desktop\New folder\" & Range("C5").Text & chr(32) & Range("C8").Text &".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End Sub
If you want to save the workbook with the macros use the below code
Sub save()
ActiveWorkbook.SaveAs Filename:="C:\Users\" & Environ$("username") & _
"\Desktop\" & Range("C5").Text & Chr(32) & Range("C8").Text & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, Password:=vbNullString, WriteResPassword:=vbNullString, _
ReadOnlyRecommended:=False, CreateBackup:=False
End Sub
if you want to save workbook with no macros and no pop-up use this
Sub save()
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="C:\Users\" & Environ$("username") & _
"\Desktop\" & Range("C5").Text & Chr(32) & Range("C8").Text & ".xls", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = True
End Sub
Ok, at that time got it done with the help of a friend and the code looks like this.
Sub Saving()
Dim part1 As String
Dim part2 As String
part1 = Range("C5").Value
part2 = Range("C8").Value
ActiveWorkbook.SaveAs Filename:= _
"C:\-docs\cmat\Desktop\pieteikumi\" & part1 & " " & part2 & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
How do I edit this part (FileFormat:= _ xlOpenXMLWorkbookMacroEnabled) for it to save as Excel 97-2013 Workbook, have tried several variations with no success.
Thankyou
Seems, that I found the solution, but my idea is flawed. By doing this FileFormat:= _ xlOpenXMLWorkbook, it drops out a popup saying, the you cannot save this workbook as a file without Macro enabled. So, is this impossible?