Select Variable Number of Worksheets to Include in a PDF

groverparkgeorge

New Member
Joined
Aug 25, 2005
Messages
33
I am automating Excel from Access. We open an Excel Template, populate one or more worksheets with data from the database and then save the Excel Template as a PDF.

I can make it all work smoothly as long as I hard-code the worksheets to be included in the PDF:

.Worksheets(Array("Bat-1", "Bat-2", "Bat-3", "Bat-4", "Bat-5")).Select
.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
ReportFiles & "\" & StrEquipmentTested & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True

However, there may be anywhere from 1 to 9 bats in any test run, so I need to select a variable number of worksheets, one for each Bat (we're talking sports equipment, not mammals).

I can create a variable that seems to be the equivalent of the string in the Array() for selecting sheets to include, but it fails with a subscript out of range error.
Is my only alternative a Case Select that counts worksheets and uses a hard-coded set of statements?

Thanks in advance.
George
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I found the answer, and it is sort of a 'doh' for those of you who use Excel a lot more than I do.

For Each xlSheet In .ActiveWorkbook.Sheets
If xlSheet.Name Like "*Bat-*" Or _
.Worksheets(iTests).Name Like "*Bat(*" Or _
.Worksheets(iTests).Name Like "*Bat (*" Then
xlSheet.Select False
End If
Next xlSheet

Because we're working with some "legacy" data, the actual worksheet names are not quite so consistent as we'd like, hence the need to include both - and ()'s as delimiters.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,517
Members
452,921
Latest member
BBQKING

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