Excel Personal workbook macro to save as pdf in current directory

Anonymous
timer Asked: Jul 7th, 2013
account_balance_wallet $5

Question Description

Excel Personal workbook macro to save as pdf in current directory

I want to record macro in personal workbook for the following.

1. Activate Book1.xlsx
2. Select sheets Sheet3, Sheet4 and Sheet6
3. Save these selected sheets as .pdf in the same directory where Book1.xls is saved
4. Name of the saved .pdf file should be the name what is written in Range A1 of Sheet1 of Book1.xlsx and suffix "20" after the name

Please provide the macro code for the same.

Thank you.

Tutor Answer

dark knight
School: University of Maryland

Thank you for the opportunity to help you with your question!

Put this code in the personal workbook:

Sub SaveBook1_AsPDF()
'workbook Book1.xlsx must already be open
'and it must have already been saved to disk
'so that it contains file path information as
'part of its name.
'That workbook must have sheets named
'Sheet1, Sheet3, Sheet4 and Sheet6 in it.
'Sheet1 must have a valid filename in cell A1.
  Dim FilePath As String
  Dim NewName As String
 
  Windows("Book1.xlsx").Activate
  FilePath = Left(ActiveWorkbook.FullName, _
  InStrRev(ActiveWorkbook.FullName, Application.PathSeparator))
  NewName = ActiveWorkbook.Worksheets("Sheet1").Range("A1")
  Sheets(Array("Sheet3", "Sheet4", "Sheet6")).Select
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  FilePath & NewName & ".pdf", Quality:=xlQualityStandard, _
  IncludeDocProperties:=True, IgnorePrintAreas:=False, _
  OpenAfterPublish:=True
End Sub

[Edit] I didn't code in the addition of the 20 suffix in the name above, you could accomplish that by changing one line of the code above like this:

  NewName = ActiveWorkbook.Worksheets("Sheet1").Range("A1") & "20"


Please let me know if you need any clarification. I'm always happy to answer your questions.

flag Report DMCA
Review

Anonymous
Excellent job

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors