VBA: Print except if rows are hidden

fannoche35 Posted messages 12 Status Member -  
cs_Le Pivert Posted messages 8437 Status Contributor -
Hello,

I created a macro that allows printing page 1 in portrait, page 2 in landscape, and then the other pages in portrait (see below Sub Imprime())

I have other macros that allow hiding entire row ranges depending on whether a checkbox is checked or not (see below Sub Checkbox10_Click()).
I would like to ensure that if my row range 96 to 167 is hidden (because checkbox10 is unchecked), there is no printing at all.
I am indeed experiencing a problem with printing blank pages.

Thank you in advance for your help.

Sub Imprime() 'Printing the 1st page in portrait'-------------------------------------- Range("A1:AX69").Select 'definition of the cell range for the 1st page ActiveSheet.PageSetup.PrintArea = "$A$1:$AX$68" 'definition of the print area ActiveSheet.PageSetup.Orientation = xlPortrait 'set to portrait mode ActiveSheet.PageSetup.CenterHorizontally = True 'horizontal centering of the sheet ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True 'printing 'Printing the 2nd page in landscape'------------------------------------- Range("A70:CM95").Select 'definition of the cell range for the 2nd page ActiveSheet.PageSetup.PrintArea = "$A$70:$CM$95" 'definition of the print area ActiveSheet.PageSetup.Orientation = xlLandscape 'set to landscape mode ActiveSheet.PageSetup.CenterHorizontally = True 'horizontal centering of the sheet ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True 'printing 'Printing the 3rd page in portrait'-------------------------------------- Range("A96:AX166").Select 'definition of the cell range for the 3rd page ActiveSheet.PageSetup.PrintArea = "$A$96:$AX$166" 'definition of the print area ActiveSheet.PageSetup.Orientation = xlPortrait 'set to portrait mode ActiveSheet.PageSetup.CenterHorizontally = True 'horizontal centering of the sheet ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True 'printing 'Printing the 4th page in portrait'-------------------------------------- Range("A167:AX237").Select 'definition of the cell range for the 4th page ActiveSheet.PageSetup.PrintArea = "$A$167:$AX$237" 'definition of the print area ActiveSheet.PageSetup.Orientation = xlPortrait 'set to portrait mode ActiveSheet.PageSetup.CenterHorizontally = True 'horizontal centering of the sheet ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True 'printing Sub CheckBox10_Click() If CheckBox10.Value = True Then Rows("96:167").EntireRow.Hidden = False Else Rows("96:167").EntireRow.Hidden = True End If End Sub

2 answers

  1. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Hello,

    like this at the beginning of Sub:

    If CheckBox10.Value = False Then Exit Sub


    --
    @+ The Woodpecker
    0
    1. fannoche35 Posted messages 12 Status Member
       
      Hello,

      I'm sorry for the delay in my response...
      Thank you for your help cs_Le Pivert
      I don't just have one checkbox but several:
      I would like pages 1 and 2 to always print
      that page 3 prints if checkbox1 is checked
      that page 4 prints if checkbox 1 or 3 or 21 is checked
      that pages 5 and 6 print if checkbox 6 is checked
      that page 7 prints if checkbox 2 is checked
      that page 8 prints if checkbox 24 is checked
      that page 9 prints if checkbox 23 is checked
      that page 10 prints if checkbox 3 is checked
      that page 11 prints if checkbox 21 is checked
      that pages 12 and 13 print if checkbox 5 is checked
      etc...
      Additionally, with my Sub Print, I can't print the pages into a single PDF document, it creates several separate documents for me. Is there a solution for that?
      Thank you in advance.
      0
  2. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Furthermore, with my Sub Print, I can't print the pages into a single PDF document; it creates several separate documents. Is there a solution for that?

    See this, you'll get the answer to your question:

    http://www.mdf-xlpages.com/modules/newbb/viewtopic.php?post_id=7055

    Then just adapt this line according to the checked CheckBoxes

     Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select


    There you go

    --
    @+ The Woodpecker
    0
    1. Jauster Posted messages 142 Status Member 42
       
      Hello,

      I recently started with VBA, and to have some fun and improve my skills, I wanted to shorten the code from fannoche35, but I had no success. Do you have any clues or ideas on why it doesn't work? Probably because of those page(i) that I declared incorrectly...

      PS: without taking checkboxes into account

      Sub Print() Dim page1 As String, page2 As String, page3 As String, page4 As String page1 = "A1:AX69" page2 = "A70:CM95" page3 = "A96:AX166" page4 = "A167:AX237" For i = 1 To 4 With ActiveSheet.PageSetup If i = 2 Then Set c = xlLandscape Else: c = xlPortrait End If .PrintArea = "page" & i 'definition of the print area .Orientation = c 'set to portrait format .CenterHorizontally = True 'horizontal centering of the sheet End With Next i 'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True 'printing End Sub 
      0
    2. cs_Le Pivert Posted messages 8437 Status Contributor 730
       
      See this for tables:

      https://silkyroad.developpez.com/vba/tableaux/#LVI

      Sub Print() Dim TableName As Variant Dim i As Integer TableName = Array("A1:AX69", "A70:CM95", "A96:AX166", "A167:AX237") For i = LBound(TableName) To UBound(TableName) MsgBox TableName(i) With ActiveSheet.PageSetup .PrintArea = TableName(i) 'definition of the print area End With Next i End Sub 


      @+
      0
    3. cs_Le Pivert Posted messages 8437 Status Contributor 730
       
      fannoche35,

      here's the principle to avoid making it overly complicated:

      You place the corresponding selection in each Sheet that will be triggered with the Worksheet Activate event like this:

      Option Explicit Private Sub Worksheet_Activate() Range("A1:AX69").Select 'definition of the cell range making up the 1st page ActiveSheet.PageSetup.PrintArea = "$A$1:$AX$68" 'definition of the print area ActiveSheet.PageSetup.Orientation = xlPortrait 'set in portrait format ActiveSheet.PageSetup.CenterHorizontally = True 'horizontal centering of the sheet End Sub 


      see the Sheet event here:

      https://silkyroad.developpez.com/VBA/FeuilleDeCalcul/#LII-A-1

      Then the caption of each CheckBox will be set to the name of the corresponding Sheet

      We'll loop through the collection of checkBoxes that will be displayed in a listbox

      https://silkyroad.developpez.com/VBA/ControlesUserForm/#LI-B

      There will also need to be a ListBox to display the selected Sheets.

      Here’s the code for 3 sheets, you’ll need to adapt it for all your sheets:

      Option Explicit 'http://www.mdf-xlpages.com/modules/newbb/viewtopic.php?post_id=7055 'http://silkyroad.developpez.com/VBA/ControlesUserForm/#LI-B 'http://silkyroad.developpez.com/VBA/FeuilleDeCalcul/#LII-A-1 Private Sub CommandButton1_Click() Dim Ctrl As Control Dim sRep As String Dim sFilename As String Dim nom As String Dim nombre As Integer 'number of sheets ListBox1.Clear 'Loop through the collection of controls set the Caption of the checkBoxes to the names of the sheets For Each Ctrl In Me.Controls If TypeOf Ctrl Is MSForms.CheckBox Then If Ctrl.Object.Value = True Then ListBox1.AddItem (Ctrl.Caption) End If End If Next Ctrl nombre = ListBox1.ListCount 'number of sheets Select Case nombre Case Is = 1 Sheets(Array(ListBox1.List(0))).Select nom = "_1" Case Is = 2 Sheets(Array(ListBox1.List(0), ListBox1.List(1))).Select nom = "_2" Case Is = 3 Sheets(Array(ListBox1.List(0), ListBox1.List(1), ListBox1.List(2))).Select nom = "_3" End Select sRep = ThisWorkbook.Path & "\" 'folder path sFilename = Replace(ThisWorkbook.Name, ".xlsm", "") 'name only without extension sFilename = sFilename & nom & ".pdf" ActiveSheet.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=sRep & sFilename, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=True MsgBox "Save in the same folder as this workbook under the name of the workbook and the number of sheets" End Sub 


      Here’s an example to download

      https://www.cjoint.com/c/HCckXMKk4QQ


      Good luck

      @+ Le Pivert
      0
    4. fannoche35 Posted messages 12 Status Member
       
      Hello cs_Le Pivert,
      I’m struggling...
      As someone who is just starting out with self-learning, it’s very challenging! I will work on it.
      Thank you for your help.
      (ps: the download link for the example is not or no longer active)
      Have a good day.
      0
    5. cs_Le Pivert Posted messages 8437 Status Contributor 730
       
      Starting out with self-training is very complicated for me! I'm going to work on it.

      What you're asking for isn't complicated, but it's a convoluted process to set up for each situation!

      (ps: the link to download the example is not or no longer active)

      It is valid for 4 days

      https://www.cjoint.com/c/HCinG0bQBSQ

      There you go
      0