VBA Looping Through Specified Sheet

amermarc

New Member
Joined
Feb 16, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All

Looking for some help with a VBA macro and looping it through set sheets (but not all) in a workbook.

Context, I have a sheet which is calculating performance against set metrics for named users. The output is a summary page (individual sheet) for which i have written the below Macro to export the summary into a PDF with a set naming convention. The problem is the current Macro requires me to run it on each of the many sheets one by one, whereas what I would like to do is run a single Macro that will create all the individual summaries on one hit.

Its also possible that from time to time I will need to adjust the named sheets as new people are added to the plan.

It was suggested to me that I could record a macro that is running each individual macro but that doesn't feel very efficient.

Any help would be much appreciated as I've spent too long trying to find a solution with no joy.


CURRENT MACRO
Sub ExportToPDFV2()
'Exports a specific cell range as a PDF and defines the output path
Dim outputPath As String
outputPath = Application.GetSaveAsFilename(Range("R3").Value & ".pdf", "PDF Files (*.pdf), *.pdf")
Range("B15:O92").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=outputPath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi and welcome to MrExcel.

With the following macro you must select the folder where all the pdfs will be stored. The name of each PDF will be taken from cell R3 of each sheet and the same range "B15:O92" of each sheet will always be saved.

In this line of the macro you must put the name of the sheets that you want to save as pdf.
Case "Sheet1", "Sheet2", "Summary", "Etc"

VBA Code:
Sub ExportToPDF()
  Dim outputPath As String, sName As String
  Dim sh As Worksheet
 
  With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select Folder"
    If .Show <> -1 Then Exit Sub
    outputPath = .SelectedItems(1) & "\"
  End With

  For Each sh In Sheets
    Select Case sh.Name
      'adjust the named sheets
      Case "Sheet1", "Sheet2", "Summary", "Etc"
        sName = sh.Range("R3").Value & ".pdf"
        sh.Range("B15:O92").ExportAsFixedFormat Type:=xlTypePDF, Filename:=outputPath & sName, _
          Quality:=xlQualityStandard, IncludeDocProperties:=True, _
          IgnorePrintAreas:=False, OpenAfterPublish:=False
    End Select
  Next
End Sub
 
Upvote 0
Sub ExportToPDF() Dim outputPath As String, sName As String Dim sh As Worksheet With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Select Folder" If .Show <> -1 Then Exit Sub outputPath = .SelectedItems(1) & "\" End With For Each sh In Sheets Select Case sh.Name 'adjust the named sheets Case "Sheet1", "Sheet2", "Summary", "Etc" sName = sh.Range("R3").Value & ".pdf" sh.Range("B15:O92").ExportAsFixedFormat Type:=xlTypePDF, Filename:=outputPath & sName, _ Quality:=xlQualityStandard, IncludeDocProperties:=True, _ IgnorePrintAreas:=False, OpenAfterPublish:=False End Select Next End Sub
Amazing thanks you so much, this seems to solve my issues, wish i'd asked earlier.
Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

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