PDA

View Full Version : [SOLVED:] Help with array printing



zoom38
08-08-2014, 12:01 PM
I have the following code that counts the worksheets that have with the name beginning with "204".



Dim ppWorksheets As Variant
Dim Count As Long
Dim WS As Worksheet

For Each WS In ThisWorkbook.Worksheets
If StrComp(Left(WS.Name, 3), "204", vbTextCompare) = 0 Then
Count = Count + 1
End If
Next WS


I have the following code that is used to send the sheets that I want to Print Preview.



ppWorksheets = Array("Cover", "Agenda", "202", "203", "204", "204 (2)", "204 (3)", "205", "206")
ThisWorkbook.Worksheets(ppWorksheets).PrintPreview
On Error Resume Next


In the print array I have to specify the sheets by name. Is there a way to combine the code above so that I don't have to specify the sheet names? Keep in mind that I am not printing all of the sheets in the workbook. I'm trying to do this because the number of "204" sheets will vary and I like the print array so I can use the print preview for multiple sheets but not all of the sheets.

Thanks
Gary

p45cal
08-08-2014, 12:40 PM
maybe something along these lines:
Dim ppWorksheets()
Dim Count As Long
Dim WS As Worksheet
ReDim ppWorksheets(0)
For Each WS In ThisWorkbook.Worksheets
If StrComp(Left(WS.Name, 3), "204", vbTextCompare) = 0 Then
ReDim Preserve ppWorksheets(Count)
ppWorksheets(UBound(ppWorksheets)) = WS.Name
Count = Count + 1
End If
Next WS
Sheets(ppWorksheets).PrintPreview

zoom38
08-08-2014, 01:09 PM
I can't say I understand how your code works, but it works great. How do I add other sheets to be previewed with them?

Gary

p45cal
08-08-2014, 01:20 PM
Dim ppWorksheets(), Count As Long, WS As Worksheet
ppWorksheets = Array("Cover", "Agenda", "202", "203", "205", "206")
Count = UBound(ppWorksheets)
For Each WS In ThisWorkbook.Worksheets
If StrComp(Left(WS.Name, 3), "204", vbTextCompare) = 0 Then
Count = Count + 1
ReDim Preserve ppWorksheets(Count)
ppWorksheets(UBound(ppWorksheets)) = WS.Name
End If
Next WS
Sheets(ppWorksheets).PrintPreview

zoom38
08-08-2014, 02:16 PM
Works perfectly.

Thank you
Gary