Save to PDF Macro Issue

mkwadvisory

New Member
Joined
Mar 1, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I run into an issue with the macro that I tried to use.

I want to save only the tabs that are named as numbers as PDF.

The code that I used isn't working fully as its does save tab named "1" and "2" as pdf but when I try to open the pdf file the adobe informs me that the file is corrupted.

I will appreciate if you could help. I also tried to assign this code to button but I can't select it.

Also is it possible to add to the code that it would save file as text contained in the cell A1 in each tab?

VBA Code:
Private Sub Filesaveas()

Dim a As Integer

Dim ws As Worksheet
Dim wb As Workbook

a = ThisWorkbook.Sheets.Count

For i = 1 To a

If ThisWorkbook.Worksheets(i).Name <> "Sheet1" And ThisWorkbook.Worksheets(i).Name <> "tab1" Then

Set wb = Workbooks.Add

ThisWorkbook.Worksheets(i).Copy before:=wb.Worksheets(1)

wb.SaveAs "C:\Users\User\OneDrive\Pictures" & "\" & ActiveSheet.Name & ".pdf"


wb.Close savechanges = True

End If

Next i


MsgBox ("ok")

1614630908221.png

1614631120148.png
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,871
Office Version
  1. 2010
Platform
  1. Windows
See if the following code works for you.
VBA Code:
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="C:\Users\User\OneDrive\Pictures" & "\" & ActiveSheet.Name & ".pdf"
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,130,033
Messages
5,639,654
Members
417,102
Latest member
bcselect

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
Top