save files as PDF for one sheet and create folders based on cell value

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
175
Office Version
  1. 2019
Platform
  1. Windows
Hello
I search macro to do:
create folder in C:\Users\RM\Desktop\Text\ based on cell G1 and create current month within created folder and save file as pdf in current month.
for instance : in G1="REPORTS"
then will become C:\Users\RM\Desktop\Text\REPORTS \
and the current month is JUNE then will become C:\Users\RM\Desktop\Text\REPORTS \JUNE\
and file name should be based on G6 for instance G6=INV NO A23455656
so will become C:\Users\RM\Desktop\Text\REPORTS \JUNE\INV NO A23455656.pdf
and should replace save file every time within created folders or create new folders if they aren't existed based on G1, current month , but I don't save sheet , just range from A:G
thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this macro:
VBA Code:
Public Sub Save_Columns_As_PDF()

    Dim mainFolder As String, subfolder As String, PDFfile As String
    Dim lastRow As Long
    
    mainFolder = "C:\Users\RM\Desktop\Text\"
    
    If Right(mainFolder, 1) <> "\" Then mainFolder = mainFolder & "\"
    
    With ActiveWorkbook.ActiveSheet
        subfolder = mainFolder & .Range("G1").Value & "\"
        If Dir(subfolder, vbDirectory) = vbNullString Then MkDir subfolder
        subfolder = subfolder & UCase(Format(Date, "mmm")) & "\"
        If Dir(subfolder, vbDirectory) = vbNullString Then MkDir subfolder
        PDFfile = subfolder & .Range("G6").Value & ".pdf"
        lastRow = .Range("A:G").Find("*", , , , xlByRows, xlPrevious).Row
        .Range("A1:G" & lastRow).ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
            Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End With
    
    MsgBox "Created " & PDFfile, vbInformation
    
End Sub
 
Upvote 1
Solution
Hi John

your code seems to work greatly except one thing , I'm not sure why do that .

if I run the code with existed MSG in end of the code it works and is stuck at running the file and I have to close EXCEL application!

and if I disable the MSG line in the end code then the file is ok

seems the procedure continues in running without any finishing when reach message !
 
Upvote 0
I know that, the problem is not when I run the macro from the first time , the problem is when try run again for new operation then doesn't show MSG and keep running without any stopping .
 
Upvote 0
seems I 've found the problem . I was using page layout from bar in the sheet bottom Which shows many pages instead of one page, I change to normal

seems taking more time to finish from many pages contain logos .
thanks very much for your code, John :)
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,105
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top