取消保护共享工作表或隐藏共享工作表中的某些内容

时间:2016-04-25 12:28:57

标签: excel vba excel-vba

我在工作簿中有一些受保护的工作表。我有一个隐藏某些行的宏,然后只打印出隐藏了特定行的特定列。 (用户需要查看屏幕上的行但不打印它们)。为了阻止打印行,我隐藏了行。为了隐藏行,我必须取消保护表单,然后在打印后保护它。下面的代码是我的。它是通过运行Print_Days()Print_Afternoons()Print_Nights()来触发的。这一切都很好,除了一个问题...如果我分享工作簿,我不能取消保护表。

是否有另一种可能在共享环境中有效的方法?

  • 我可以只打印所需的行吗?我试过了,但是它会在不同的页面上打印行而不是一起打印,就好像它是一系列单元格一样。
  • 我可以将所需内容复制到另一个工作簿并打印吗?如果是这样,我不知道该怎么做!
  • 有更好的方法吗?

Public c1
Public c2
Public c3
Public c4

Sub Print_Days()
    c1 = "b"
    c2 = "c"
    c3 = "d"
    c4 = "e"
    Call Print_Schedule(4)
End Sub

Sub Print_Afternoons()
    c1 = "g"
    c2 = "h"
    c3 = "i"
    c4 = "j"
    Call Print_Schedule(1)
End Sub

Sub Print_Nights()
    c1 = "l"
    c2 = "m"
    c3 = "n"
    c4 = "o"
    Call Print_Schedule(1)
End Sub

Sub Print_Schedule(Print_Copies)
    Call UnProtectSheet


Dim r As Long
  For r = 38 To 190
    'Hide empty rows
    If Application.CountA(Range(c1 & r & ":" & c1 & r)) = 0 Then
      Range("A" & r).EntireRow.Hidden = True
    End If

    'Hide UP rows
    If Range(c3 & r) = "UP" Or Range(c3 & r) = "VAC" Or Range(c3 & r) = "OFF" Or Range(c3 & r) = "NCNS" Or Range(c3 & r) = "AA" Or Range(c3 & r) = "AB - LOA" Then
  Range("A" & r).EntireRow.Hidden = True
End If

  Next r

 'Save the current print area
 curPrtArea = ActiveSheet.PageSetup.PrintArea
 'Save the current orientation
 curOrientation = ActiveSheet.PageSetup.Orientation
 'Save the current print color profile
 curPrtColor = ActiveSheet.PageSetup.BlackAndWhite
 'Define the setting to only print Black and White
 ActiveSheet.PageSetup.BlackAndWhite = True
  'Define desired print orientation
  ActiveSheet.PageSetup.Orientation = xlPortrait
  ActiveSheet.PageSetup.PaperSize = xlPaperLegal
  ActiveSheet.PageSetup.Zoom = False
  ActiveSheet.PageSetup.FitToPagesTall = 1
 'Define desired print area
  myPrtArea = c1 & "37:" & c4 & "190"
 'Set the desired print area
   ActiveSheet.PageSetup.PrintArea = myPrtArea
 'Print the desired print area
   ActiveSheet.PrintOut Copies:=Print_Copies

 'Unhide all rows
 Range("A38:A190").EntireRow.Hidden = False

 'Reset the original print orientation
  ActiveSheet.PageSetup.Orientation = curOrientation
 'Reset the original print area
 ActiveSheet.PageSetup.PrintArea = curPrtArea
 ActiveSheet.PageSetup.BlackAndWhite = curPrtColor


    Call ProtectSheet_All_Parameters_passed
End Sub


Function UnProtectSheet()
'UnProtect Method without a password passed
    ActiveSheet.Unprotect
End Function

Function ProtectSheet_All_Parameters_passed()
 'Protect Method with all the parameters passed in it
    ActiveSheet.Protect _
        Password:="", _
        DrawingObjects:=False, _
        Contents:=True, _
        Scenarios:=True, _
        UserInterfaceOnly:=True, _
        AllowFormattingCells:=True, _
        AllowFormattingColumns:=False, _
        AllowFormattingRows:=False, _
        AllowInsertingColumns:=False, _
        AllowInsertingRows:=False, _
        AllowInsertingHyperlinks:=False, _
        AllowDeletingColumns:=False, _
        AllowDeletingRows:=False, _
        AllowSorting:=False, _
        AllowFiltering:=False, _
        AllowUsingPivotTables:=False
    ActiveSheet.EnableSelection = xlUnlockedCells
End Function

1 个答案:

答案 0 :(得分:0)

您应该查看xlSheetVeryHidden。这可以防止用户访问工作表,同时仍允许您在共享工作簿中进行打印。这是工作表可以拥有的第三个状态,以及xlVisible和xlHidden。您可以使用代码在需要打印的xlVisible和保护所需的xlVeryHidden之间移动。

杰森