+ Reply to Thread
Results 1 to 7 of 7

Thread: Hide rows before printing

  1. #1
    Registered User
    Join Date
    03-08-2011
    Location
    Minneapolis, Minnesota, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Hide rows before printing

    Hello,

    This code was working and now is not. We have a mixed environment of Excel 2003 and Excel 2010. It was working on both until recently.
    The MSG box pops up normally, so I know that the code is being parsed, but the Hiding and Un-hiding of rows is not.

    Private Sub Workbook_Open()
    
    MsgBox "The information in this workbook is privileged, and strictly confidential it is intended solely for the use of Wellington Security Systems. If the reader of this message is not an employee or agent of Wellington Security Systems, dissemination, distribution, copying or other use of the information contained in this workbook is strictly prohibited. If you have received this workbook and you were not the original intended recipient, please first notify the sender immediately and then delete this workbook from all data storage devices and destroy all hard copies.", vbExclamation, "Confidential Information"
    
    End Sub
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
         
         Dim Firstrow As Long
         Dim Lastrow As Long
         
         With ActiveSheet
          
          Firstrow = .UsedRange.Cells(1).Row
          Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    
         End With
         
      If LCase(ActiveSheet.Name) = "burg - fire - access" Then
        
       Cancel = True
      
       Application.EnableEvents = False
       Application.ScreenUpdating = False
      
        With ActiveSheet
         
         Rows.EntireRow.Hidden = False
         
         For RowCnt = Firstrow To Lastrow
            If Cells(RowCnt, 1).Value <> "x" Then
               Cells(RowCnt, 1).EntireRow.Hidden = True
            End If
         Next RowCnt
        
        .ResetAllPageBreaks
        .PageSetup.Zoom = 80
         
        .PrintOut
        
        Rows.EntireRow.Hidden = False
        
      End With
     
      Application.EnableEvents = True
      Application.ScreenUpdating = True
      
     ElseIf LCase(ActiveSheet.Name) = "rsi" Then
        
       Cancel = True
      
       Application.EnableEvents = False
       Application.ScreenUpdating = False
      
        With ActiveSheet
         
         Rows.EntireRow.Hidden = False
         
         For RowCnt = Firstrow To Lastrow
            If Cells(RowCnt, 1).Value <> "x" Then
               Cells(RowCnt, 1).EntireRow.Hidden = True
            End If
         Next RowCnt
        
        .ResetAllPageBreaks
        .PageSetup.Zoom = 80
         
        .PrintOut
        
        Rows.EntireRow.Hidden = False
        
      End With
     
      Application.EnableEvents = True
      Application.ScreenUpdating = True
       
     ElseIf LCase(ActiveSheet.Name) = "cctv" Then
         
       Cancel = True
      
       Application.EnableEvents = False
       Application.ScreenUpdating = False
      
        With ActiveSheet
        
         Rows.EntireRow.Hidden = False
         
         For RowCnt = Firstrow To Lastrow
            If Cells(RowCnt, 1).Value <> "x" Then
               Cells(RowCnt, 1).EntireRow.Hidden = True
            End If
         Next RowCnt
        
        .ResetAllPageBreaks
        .PageSetup.Zoom = 80
         
        .PrintOut
        
        Rows.EntireRow.Hidden = False
        
      End With
     
      Application.EnableEvents = True
      Application.ScreenUpdating = True
      
     ElseIf LCase(ActiveSheet.Name) = "aes intellinet" Then
         
       Cancel = True
      
       Application.EnableEvents = False
       Application.ScreenUpdating = False
      
        With ActiveSheet
         
         Rows.EntireRow.Hidden = False
         
         For RowCnt = Firstrow To Lastrow
            If Cells(RowCnt, 1).Value <> "x" Then
               Cells(RowCnt, 1).EntireRow.Hidden = True
            End If
         Next RowCnt
        
        .ResetAllPageBreaks
        .PageSetup.Zoom = 80
         
        .PrintOut
        
        Rows.EntireRow.Hidden = False
        
      End With
     
      Application.EnableEvents = True
      Application.ScreenUpdating = True
       
     Else
      
       Cancel = False
        
     End If
      
     
    End Sub
    Briefly, this is a sales workup with parts numbers and quantities, this allows all of the parts to be listed, and only print a page with parts that have a quantity greater than 0. There is a simple formula in column A which places an X in rows which have a number greater than 0 in the quantity cell.

    What's happening now is simply that the entire worksheet is printing, it is not hiding the rows as it used to.

    Thanks in advance for looking.
    Last edited by DonkeyOte; 03-09-2011 at 03:00 AM.

  2. #2
    Registered User
    Join Date
    03-08-2011
    Location
    Minneapolis, Minnesota, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Hide rows before printing

    I think maybe this post should be in another section related to VBA, but I don't want to cross post...

    Can a moderator move this for me?

    Thanks

  3. #3
    Valued Forum Contributor
    Join Date
    08-23-2009
    Location
    Hampshire, UK
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    324

    Re: Hide rows before printing

    I've tried your code in a dummy workbook and it hides the rows correctly. Have you checked that the sheet names haven't been changed?

  4. #4
    Registered User
    Join Date
    03-08-2011
    Location
    Minneapolis, Minnesota, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Hide rows before printing

    I'm attaching the workbook to see if there's something there that I've missed.

    For simplicity, I've deleted some of the worksheets as there is some effort to scrub the data for public viewing.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    08-23-2009
    Location
    Hampshire, UK
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    324

    Re: Hide rows before printing

    That works ok for me - it only prints the top few rows unless I add more x's. I don't know why it's not working for you, sorry. Perhaps someone else can suggest a reason.

  6. #6
    Registered User
    Join Date
    03-08-2011
    Location
    Minneapolis, Minnesota, USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Hide rows before printing

    Ughh... I don't understand. I've tried it again myself, and it still doesn't work.

    It worked fine before on both Excel 2010 and 2003, but it now doesn't work in either environment across multiple PCs. No policy changes have been made and no software updates (unless someone knows of any Microsoft security patches which might have broken it on my end.)

    If anyone has any ideas of setting I can change on my end to get this functioning again I would greatly appreciate it.

    Thanks again.

  7. #7
    Forum Guru
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2003
    Posts
    2,288

    Re: Hide rows before printing

    It works for me.
    You need to step through the code on your machine, this allows you to validate each line of code in turn: There are a couple of minor issues which may cause problems - so we'll also use some neatened up code.
    Select your burg - fire - access sheet
    Open the VB editor - Alt+F11
    Replace your macro with this (leave the open workbook event):
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    End Sub
    Sub test()
    
    Dim rCell As Range
    
    With ActiveSheet
        Select Case LCase(.Name)
        Case "burg - fire - access", "rsi", "cctv", "aes intellinet"
        
            Cancel = True
            Application.EnableEvents = False
            Application.ScreenUpdating = False
            
            .Cells.EntireRow.Hidden = False
            For Each rCell In .UsedRange.Columns(1).Cells
                If rCell <> "x" Then rCell.EntireRow.Hidden = True
            Next rCell
            
            .ResetAllPageBreaks
            .PageSetup.Zoom = 80
            '.PrintOut
            
            .Cells.EntireRow.Hidden = False
            Application.EnableEvents = True
            Application.ScreenUpdating = True
        End Select
    End With
    
    End Sub
    Make sure the cursor is below the 'Sub test()' line.
    Press F8 - this starts running the macro in break mode (i.e. it waits for you to continue).
    Arrange windows so you can see both the VBE and the Excel workbook - so you can watch it manipulate the rows (or not...)
    Press F8 repeatedly, watching the effect of each line - the first thing to look for is when you get to this line:
            .Cells.EntireRow.Hidden = False
    Tell us how it goes...
    Last edited by Cheeky Charlie; 03-10-2011 at 01:33 AM. Reason: afterthoughts before forethoughts
    CC


    If you feel really indebted please consider a donation to charity. My preferred charity is ActionAid but there are plenty of worthy alternatives.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0