Dynamically select sheets based on values

JonathanA

Board Regular
Joined
Jan 2, 2008
Messages
62
Hi there,

I'm sure this is a simple problem to some of the geniuses on here but I don't know how to do it.
I want to be able to dynamically select the sheets for printing based on a variable, I have set these as Boolean for the time being but they could be anything as they are just generated as whether certain cells are greater than zero. So in a nutshell I have 4 variables (There will be more hence my need for the dynamic bit) that relate to whether a sheet should be included in the Sheets(array(...)).select statement at the end to save to PDF. A shortened version of what I have so far is included below, you can see that for more than 4 variables it's going to be a lot of If statements.
Code:
Dim NotBoughts As Boolean
Dim Samples As Boolean
Dim Emails As Boolean
Dim Phone As Boolean


If NotBoughts = True And Samples = True And Emails = True And Phone = True Then
     
    Sheets(Array("Report", "Not Boughts", "Samples", "Emails", "Phone")).Select
    myfile = "M:\JonA\Secure\" & pi.Name & " WE" & Format(mydate, "ddmmyyyy") & ".pdf"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myfile, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False
      Else:

This is run within a loop for each salesperson and I'd like to be able to generate an array for the
Sheets(Array("Report", "Not Boughts", "Samples", "Emails", "Phone")).Select
based on whether the relevant variables are true or not, print if true.

So if only Samples is true then the array would end up
Sheets(Array("Report","Samples")).Select ( Report is the headline page and is always printed, pdf'd)

Any tips greatly appreciated
Jon
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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