Sub SaveAsPDF()
' Get the path of the current workbook
Dim path As String
path = ThisWorkbook.Path & "\"
' Create a new folder with the current date and time as the name
Dim folderName As String
folderName = Format(Now(), "yyyy-mm-dd_hh-mm-ss")
MkDir path & folderName
' Save the current worksheet as a PDF in the new folder
Dim fileName As String
fileName = folderName & "\" & "MyWorksheet.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & fileName
' Display a message box to confirm the save
MsgBox "PDF saved in folder: " & path & folderName
End Sub
Thank you very much. Really this is what is needed and it works very wellI asked an AI website your question. The response received (untested) :
Sure, here's some VBA code that should accomplish what you're looking for:
VBA Code:Sub SaveAsPDF() ' Get the path of the current workbook Dim path As String path = ThisWorkbook.Path & "\" ' Create a new folder with the current date and time as the name Dim folderName As String folderName = Format(Now(), "yyyy-mm-dd_hh-mm-ss") MkDir path & folderName ' Save the current worksheet as a PDF in the new folder Dim fileName As String fileName = folderName & "\" & "MyWorksheet.pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & fileName ' Display a message box to confirm the save MsgBox "PDF saved in folder: " & path & folderName End Sub
This code will create a new folder in the same path as the current workbook, using the current date and time as the folder name. It will then save the current worksheet as a PDF in that folder, with the filename "MyWorksheet.pdf". Finally, it will display a message box to confirm that the PDF has been saved.
You can modify the code to change the folder name or PDF filename to whatever you like. If you want to save a different worksheet, you'll need to modify the `ActiveSheet` reference to point to the worksheet you want to save.