【发布时间】: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 作为前端。