Jyggalag

Active Member
Joined
Mar 8, 2021
Messages
445
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi all,

I currently have a macro that converts my excel sheet into a PDF and saves it in a designated folder. However, I only want it to save the first sheet as a PDF, and ignore my other sheets, which it will not do at the moment.

My code looks as follows:

VBA Code:
Option Explicit

Sub SaveFileWithMacro()

Dim Path As String
Dim fn As String
Path = "S:\PATH\Tracking\PDF files\"
fn = Range("A63")
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Path & fn & ".pdf"

End Sub

A63 is the cell in which I type in the name of the PDF file to be saved.

Can anybody help me fix my macro so it only saves the first sheet in the below picture (the one called "Sheet3")?

Thank you! :)

1655882378303.png
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try
Rich (BB code):
Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"

BTW, which sheet is the A63 with the pdf name in?
 
Upvote 0
Try
Rich (BB code):
Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"
Thank you Peter!

I am still very inexperienced in VBA and am a bit scared to make any harsh edits, if you do not mind, do I replace this piece with code already there, or do I insert it on top of it (and if so, where?).

Kind regards,
Jyggalag
 
Upvote 0
Take out the red line and insert the blue one
Rich (BB code):
Sub SaveFileWithMacro()

Dim Path As String
Dim fn As String
Path = "S:\PATH\Tracking\PDF files\"
fn = Range("A63")
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Path & fn & ".pdf"
Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"
End Sub
 
Upvote 0
Take out the red line and insert the blue one
Rich (BB code):
Sub SaveFileWithMacro()

Dim Path As String
Dim fn As String
Path = "S:\PATH\Tracking\PDF files\"
fn = Range("A63")
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Path & fn & ".pdf"
Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"
End Sub
Thank You Peter!

I am currently getting this debug error however:

1655884487777.png


Is it because my sheet is named Sheet3 and maybe I need to mention this somehow?
 
Upvote 0
Is it because my sheet is named Sheet3 and maybe I need to mention this somehow?
It shouldn't be, but see further questions below.

What is the answer to my earlier question?
BTW, which sheet is the A63 with the pdf name in?

Also,
- which sheet is the active sheet when the code is run?
- does your workbook have any hidden sheets?
 
Upvote 0
H
It shouldn't be, but see further questions below.

What is the answer to my earlier question?


Also,
- which sheet is the active sheet when the code is run?
- does your workbook have any hidden sheets?
Hi Peter,

Thanks for your quick response.

The cell A63 is also in Sheet3 (apologies for not mentioning this before) and the code is run through a macro button in Sheet3 as well.

I have no hidden sheets. So only the four sheets shown in the first photo

Hope this makes sense? :)
 
Upvote 0
Then the most likely problem is either a value in cell A63 that is not a valid file name or a file path that does not exist.
 
Upvote 0
the code is run through a macro button in Sheet3 as well.
In that case you should also be able to swap these lines (though the red one should still work if the sheet is definitely the first sheet in the workbook).

Rich (BB code):
Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"
 
Upvote 0
Solution
In that case you should also be able to swap these lines (though the red one should still work if the sheet is definitely the first sheet in the workbook).

Rich (BB code):
Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fn & ".pdf"
I do not think there is an issue with the name in cell A63, it worked before and was able to save the file (although with every sheet in the excel file).

I replaced it now, but it still debugs and highlights this:

1655887862243.png


Did I perhaps misunderstand something?

Thanks again :)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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