Macro to select all worksheet and save file as file name

Luis Ferreira

New Member
Joined
Jan 2, 2012
Messages
13
Good Afternoon,

I want to create a macro that will select all the worksheets (names and quantity will vary) and saves the file as the current file's name but in PDF. Since I only know how to record a macro it specifies the worksheet names but I need it for various workbooks. The name will vary plus the number of tabs can go anywhere from 3 to 40.

Thank you,

Luis
 
Oh! Al-hamdullah, everything is OK :)

I am currently working on modifying the code that considers exaclty what you have just said, i.e., saving the PDF to the workbook folder.

But in this case I have to remove the folder selector window from the code. Correct?

Will revert to you soon, in-sha' Allah
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Yes, that is it.

I had a muslim friend that thought me some, but I can only speak it. I will try to type it but forgive me if something else comes out

Asah Walenkam (hope that it is a good try)
 
Upvote 0
Good for a first try, but could not get it :) May be you were trying to say Al-salamu Alaikum? Don't worry.

Here you go, try this version

Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Option Explicit

Sub SaveWorkbookAsPDF()

    Dim sFileName As String, _
        sFolderName As String

    [COLOR="Green"]'* Get the folder of ThisWorkbook[/COLOR]
    sFolderName = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))

    [COLOR="green"]'* Check if file already exists[/COLOR]
    sFileName = Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".") - 1) + ".pdf"
    If Dir(sFolderName + sFileName, vbNormal) = sFileName Then
        If MsgBox("The file """ + sFolderName + sFileName + """ already exists." + _
            Chr(10) + "Do you want to overwrite it?", vbYesNo, "File Exists") = vbNo Then
            Exit Sub
        End If
    End If

    sFileName = sFolderName + sFileName
    [COLOR="green"]'* Save as PDF[/COLOR]
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=sFileName, Quality:=xlQualityStandard, _
        OpenAfterPublish:=True

End Sub[/COLOR][/SIZE][/FONT]
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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