Print routine not working with locked cells

Ian Bertie

New Member
Joined
Jul 10, 2010
Messages
21
Good morning Guru's

I have created a print routine as below to hide unused rows, however if I try to lock certain cells to prevent change, the routine fails with the error message "Run time error 1004 Unable to set the hidden property of the range class"

Sub Print_NonBlank_Rows()
Dim rng As Range, cell As Range, toHide As Range
Dim r&, c%, b%, n%
Set rng = ActiveSheet.UsedRange
For r = 1 To rng.Rows.Count
b = 0
For c = 1 To rng.Columns.Count
If rng.Cells(r, c).Value = "" Then
b = b + 1
End If
Next c
If b = rng.Columns.Count Then
If n = 0 Then
Set toHide = rng.Cells(r, c).EntireRow
n = 1
Else
Set toHide = Union(toHide, rng.Cells(r, c).EntireRow)
End If
End If
Next r
toHide.EntireRow.Hidden = True
ActiveSheet.PrintOut Copies:=1
toHide.EntireRow.Hidden = False
End Sub


Any ideas please
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hello there. You will have to temporarily unlock the worksheet, run your code, then lock it again. Since most of your code will work on the protected sheet, I have only unprotected it for the minimum number of steps. Try this (untested):
VBA Code:
Sub Print_NonBlank_Rows()
    Dim rng As Range, cell As Range, toHide As Range
    Dim r&, c%, b%, n%
    Set rng = ActiveSheet.UsedRange
    For r = 1 To rng.Rows.Count
        b = 0
        For c = 1 To rng.Columns.Count
            If rng.Cells(r, c).Value = "" Then
                b = b + 1
            End If
        Next c
        If b = rng.Columns.Count Then
            If n = 0 Then
                Set toHide = rng.Cells(r, c).EntireRow
                n = 1
            Else
                Set toHide = Union(toHide, rng.Cells(r, c).EntireRow)
            End If
        End If
    Next r
    ActiveSheet.Unprotect
  
    toHide.EntireRow.Hidden = True
    ActiveSheet.PrintOut Copies:=1

    toHide.EntireRow.Hidden = False
    ActiveSheet.Protect
End Sub
 
Upvote 0
Solution
Hello there. You will have to temporarily unlock the worksheet, run your code, then lock it again. Since most of your code will work on the protected sheet, I have only unprotected it for the minimum number of steps. Try this (untested):
VBA Code:
Sub Print_NonBlank_Rows()
    Dim rng As Range, cell As Range, toHide As Range
    Dim r&, c%, b%, n%
    Set rng = ActiveSheet.UsedRange
    For r = 1 To rng.Rows.Count
        b = 0
        For c = 1 To rng.Columns.Count
            If rng.Cells(r, c).Value = "" Then
                b = b + 1
            End If
        Next c
        If b = rng.Columns.Count Then
            If n = 0 Then
                Set toHide = rng.Cells(r, c).EntireRow
                n = 1
            Else
                Set toHide = Union(toHide, rng.Cells(r, c).EntireRow)
            End If
        End If
    Next r
    ActiveSheet.Unprotect
 
    toHide.EntireRow.Hidden = True
    ActiveSheet.PrintOut Copies:=1

    toHide.EntireRow.Hidden = False
    ActiveSheet.Protect
End Sub
Thank you John
This works, the only change I made was to add the password so the user does not need to enter
Regards
Ian
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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