I have a list of 11 worksheets (Sheet1, Sheet2, Sheet3, etc.) in an Excel Workbook. I need to be able to select from that list, a set of sheets to print as one file. The names of the Sheets will not change.
So if I want to print all 11 at once, I would put:
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4","Sheet5","Sheet6","Sheet7","Sheet8","Sheet9","Sheet10","Sheet11",).PrintOut
but instead of this, I want to choose any combination of the 11 to print out.
ThisWorkbook.Sheets(Array("Sheet3", "Sheet5", "Sheet7,"Sheet9","Sheet11").PrintOut
My goal has been to make the workbook user-friendly, so I have set up checkboxes that will unhide those specific sheets if the checkbox control cell is TRUE. Quick illustration:
If Sheets("ControlSheet").Range("A1").Value = TRUE then
Sheets("Sheet1").Visible = TRUE
'where ControlSheet Range A1 is the cell linked to the checkbox for that sheet.
I would like to be able to use those same checkboxes to select which sheets will print.
My specific issue: I would like to generate a dynamic array in VBA based on cell values in a worksheet, and be able to use that array to print specific worksheets as one file.
Very open to other suggestions if you think maybe I am making this overly-complex for what I need. Appreciate any and all help!
So just print the non-hidden sheets from that list of 11?
Sub PrintVisibleSheets()
Dim arr, s, lst, sep
arr = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", _
"Sheet6", "Sheet7", "Sheet8", "Sheet9", "Sheet10", "Sheet11")
For Each s In arr
If ThisWorkbook.Sheets(s).Visible Then
lst = lst & sep & s
sep = "|" 'add separator after first pass
End If
Next s
'any visible sheets?
If Len(lst) > 0 Then ThisWorkbook.Sheets(Split(lst, "|")).PrintOut
End Sub
Final Code below! Hope this can help someone else!
Sub PrintVisibleSheets()
If Sheets("ControlSheet").Range("C32").Value = True Then
Sheets("Sheet1").Visible = True
End If
'next
If Sheets("ControlSheet").Range("C33").Value = True Then
Sheets("Sheet2").Visible = True
End If
'next
If Sheets("ControlSheet").Range("C34").Value = True Then
Sheets("Sheet3").Visible = True
End If
'next
If Sheets("ControlSheet").Range("C35").Value = True Then
Sheets("Sheet4").Visible = True
End If
'next
If Sheets("ControlSheet").Range("C36").Value = True Then
Sheets("Sheet5").Visible = True
End If
'next
If Sheets("ControlSheet").Range("C37").Value = True Then
Sheets("Sheet6").Visible = True
End If
'next
If Sheets("ControlSheet").Range("C38").Value = True Then
Sheets("Sheet7").Visible = True
End If
'next
''''''''''''
'''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Hide Sheets if False
If Sheets("ControlSheet").Range("C32").Value = True Then
Sheets("Sheet1").Visible = True
End If
'next
If Sheets("ControlSheet").Range("C33").Value = False Then
Sheets("Sheet2").Visible = False
End If
'next
If Sheets("ControlSheet").Range("C34").Value = False Then
Sheets("Sheet3").Visible = False
End If
'next
If Sheets("ControlSheet").Range("C35").Value = False Then
Sheets("Sheet4").Visible = False
End If
'next
If Sheets("ControlSheet").Range("C36").Value = False Then
Sheets("Sheet5").Visible = False
End If
'next
If Sheets("ControlSheet").Range("C37").Value = False Then
Sheets("Sheet6 ").Visible = False
End If
'next
If Sheets("ControlSheet").Range("C42").Value = False Then
Sheets("Sheet7").Visible = False
End If
Dim arr, s, lst, sep
arr = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7")
For Each s In arr
If ThisWorkbook.Sheets(s).Visible Then
lst = lst & sep & s
sep = "|"
End If
Next s
If Len(lst) > 0 Then
Application.Dialogs(xlDialogPrinterSetup).Show
ThisWorkbook.Sheets("Sheet1").PageSetup.PrintQuality = 600
ThisWorkbook.Sheets("Sheet2").PageSetup.PrintQuality = 600
ThisWorkbook.Sheets("Sheet3").PageSetup.PrintQuality = 600
ThisWorkbook.Sheets("Sheet4").PageSetup.PrintQuality = 600
ThisWorkbook.Sheets("Sheet5").PageSetup.PrintQuality = 600
ThisWorkbook.Sheets("Sheet6").PageSetup.PrintQuality = 600
ThisWorkbook.Sheets("Sheet7").PageSetup.PrintQuality = 600
ThisWorkbook.Sheets(Split(lst, "|")).PrintOut
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.