Change this macro so it selects sheets from a list

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I have a macro that selects two sheets and creates a PDF, its exactly what I need except I need it to pick a selection of sheets to use from a list,
I have a sheet called "Control"

Range is T2:U20

In column T I have a list of sheet names, in column U I have a Yes or No,
I want each Sheet that has a yes next to it to be included in the PDF.

Code:
Sub MultiPDF()

Sheets("Dashboard").Select
Sheets("Dashboard2").Select False       'select both sheets for PDF

Application.ScreenUpdating = False

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=ActiveWorkbook.Path & "\" & ActiveSheet.Range("AF5").Value & " " & Format(Now, "MM-DD-YYYY") & ".pdf", _
    OpenAfterPublish:=True
Application.ScreenUpdating = True
End Sub
Heres the code I have,

Please help if you can

Thanks

Tony
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Does this work?

Code:
Sub MultiPDF()


  Dim Cel As Range
  Dim PDFShtRng As Range
  Dim Cntrl As Worksheet
  Dim Sht As Worksheet
  Dim A As String
  
  Set Cntrl = Worksheets("Control")
  Set Cel = Cntrl.Range("T2")
  Set PDFShtRng = Cntrl.Range(Cel, Cntrl.Cells(Cntrl.Cells.Rows.Count, Cel.Column).End(xlUp))
  
  Application.ScreenUpdating = False
  
  For Each Cel In PDFShtRng
    If UCase(Cel.Offset(0, 1).Value) = "YES" Then
      Set Sht = Worksheets(Cel.Value)
      
      Sht.ExportAsFixedFormat Type:=xlTypePDF, _
      Filename:=ActiveWorkbook.Path & "\" & Sht.Range("AF5").Value & " " & Format(Now, "MM-DD-YYYY") & ".pdf", _
        OpenAfterPublish:=True
    End If
  Next Cel
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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