【问题标题】:Unprotect shared worksheet OR hide certain content in a shared worksheet取消保护共享工作表或隐藏共享工作表中的某些内容
【发布时间】:2016-04-25 12:28:56
【问题描述】:

我的工作簿中有一些受保护的工作表。我有一个隐藏某些行的宏,然后只打印出隐藏特定行的特定列。 (用户需要看到屏幕上的行,但不需要打印)。为了停止打印行,我隐藏了行。为了隐藏行,我必须取消保护工作表,然后在打印后保护它。下面的代码是我所拥有的。它由运行Print_Days()Print_Afternoons()Print_Nights() 触发。除了一个问题外,一切都很好......如果我共享工作簿,我无法取消保护工作表。

还有其他方法可以在共享环境中使用吗?

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

.

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

【问题讨论】:

  • IMO:最好的方法是在 Excel 文件后面使用 SQL。 Excel 中没有真正的保护/安全性(一旦您允许某人打开文件)!如果您只想显示相关数据或允许某些用户查看的数据,则设置 SQL 来处理数据/安全性并仅使用 Excel 作为前端。

标签: vba excel


【解决方案1】:

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

杰森

【讨论】:

  • 我更新了原始问题以解释我不需要隐藏整个工作表......只有某些单元格并且只有在打印时。如果我理解您的回答...它将隐藏整个工作表...对吗?
  • 在顶部的页面布局选项卡上,将打印区域设置为仅包含所需的行。然后你不需要隐藏或取消隐藏这些行。然后进入视图标签,点击分页预览,调整分页。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 2018-08-03
  • 2021-12-10
  • 1970-01-01
  • 2017-03-24
相关资源
最近更新 更多