Excel 当DrawingObject:=false时,图纸不受保护
在受保护的工作表上,我有一个验证列表,当某个范围内的值发生更改时,该列表将使用VBA代码进行动态更新。通过“工作表更改”事件调用此函数。首先我调用RemoveProtect,然后调用MakeValidateList,后面是EnableProtectExcel 当DrawingObject:=false时,图纸不受保护,excel,excel-2007,vba,Excel,Excel 2007,Vba,在受保护的工作表上,我有一个验证列表,当某个范围内的值发生更改时,该列表将使用VBA代码进行动态更新。通过“工作表更改”事件调用此函数。首先我调用RemoveProtect,然后调用MakeValidateList,后面是EnableProtect Public Sub RemoveProtect() If ActiveSheet.ProtectContents = True Then Application.ScreenUpdating = False ActiveWorkb
Public Sub RemoveProtect()
If ActiveSheet.ProtectContents = True Then
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect
ActiveSheet.Unprotect
Application.ScreenUpdating = True
End If
End Sub
Public Function makeValidateList(ByVal cell As Range, ByVal r1 As Range) As Integer
Dim arrCargo() As String
Dim i, c As Integer
ReDim arrCargo(1)
arrCargo(0) = "SLOPS" 'vaste waarden
arrCargo(1) = "MT"
c = UBound(arrCargo) + 1
For i = 1 To r1.Count
If r1.Cells(i, 1).Value <> "" Then
ReDim Preserve arrCargo(UBound(arrCargo) + 1)
arrCargo(c) = r1.Cells(i, 1).Value
c = c + 1
End If
Next i
With cell.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(arrCargo, ",")
.IgnoreBlank = True
.InCellDropdown = True
End With
End Function
Public Sub EnableProtect()
If ActiveSheet.Protect = False Then
Application.ScreenUpdating = False
ActiveWorkbook.Protect
ActiveSheet.Protect UserInterfaceOnly:=True, DrawingObjects:=False
Application.ScreenUpdating = True
End If
End Sub
Public Sub-RemoveProtect()
如果ActiveSheet.ProtectContents=True,则
Application.ScreenUpdating=False
ActiveWorkbook.Unprotect
活动表。取消保护
Application.ScreenUpdating=True
如果结束
端接头
公共函数makeValidateList(ByVal单元格作为范围,ByVal r1作为范围)作为整数
Dim arr()作为字符串
作为整数的Dim i,c
重装货物(1)
arrCargo(0)=“SLOPS”vaste waarden
空运货物(1)=“公吨”
c=UBound(arrcoard)+1
对于i=1到r1。计数
如果r1.单元格(i,1).值为“”,则
ReDim保存货物(UBound(arrcoard)+1)
arrchargo(c)=r1.单元格(i,1).值
c=c+1
如果结束
接下来我
带单元格。验证
.删除
.Add类型:=xlValidateList,公式1:=Join(arrchargo,,“”)
.IgnoreBlank=True
.InCellDropdown=True
以
端函数
公共子启用保护()
如果ActiveSheet.Protect=False,则
Application.ScreenUpdating=False
ActiveWorkbook.Protect
ActiveSheet.Protect UserInterfaceOnly:=True,DrawingObject:=False
Application.ScreenUpdating=True
如果结束
端接头
对于drawingobjects:=false,工作表将保持不受保护状态,单元格不会锁定,公式也不会隐藏。
删除drawingobjects:=false时,将保护图纸并隐藏公式。但是validatelist没有更新
我做错了什么?请尝试以下代码:
Const strPassWord As String = "1234"
Public Function makeValidateList(ByVal cell As Range, ByVal r1 As Range) As Integer
Dim arrCargo() As String
Dim i, c As Integer
ReDim arrCargo(1)
arrCargo(0) = "SLOPS" 'vaste waarden
arrCargo(1) = "MT"
c = UBound(arrCargo) + 1
For i = 1 To r1.Count
If r1.Cells(i, 1).Value <> "" Then
ReDim Preserve arrCargo(UBound(arrCargo) + 1)
arrCargo(c) = r1.Cells(i, 1).Value
c = c + 1
End If
Next i
With cell.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(arrCargo, ",")
.IgnoreBlank = True
.InCellDropdown = True
End With
End Function
Sub EnableProtect()
'Assumed Sheets("Sheet1") change it if needed
Sheets("sheet1").Range("B1:B100").Locked = False ' You can alter this range as per your requirement
Sheets("sheet1").Protect Password:=strPassWord, DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Sub RemoveProtect()
Sheets("sheet1").Unprotect Password:=strPassWord
End Sub
Const strPassWord As String=“1234”
公共函数makeValidateList(ByVal单元格作为范围,ByVal r1作为范围)作为整数
Dim arr()作为字符串
作为整数的Dim i,c
重装货物(1)
arrCargo(0)=“SLOPS”vaste waarden
空运货物(1)=“公吨”
c=UBound(arrcoard)+1
对于i=1到r1。计数
如果r1.单元格(i,1).值为“”,则
ReDim保存货物(UBound(arrcoard)+1)
arrchargo(c)=r1.单元格(i,1).值
c=c+1
如果结束
接下来我
带单元格。验证
.删除
.Add类型:=xlValidateList,公式1:=Join(arrchargo,,“”)
.IgnoreBlank=True
.InCellDropdown=True
以
端函数
子启用保护()
'假定工作表(“工作表1”)根据需要进行更改
图纸(“图纸1”)。范围(“B1:B100”)。锁定=假“您可以根据需要更改此范围
工作表(“sheet1”)。保护密码:=strPassWord,绘图对象:=True,内容:=True,场景:=True
端接头
子删除保护()
工作表(“sheet1”)。取消保护密码:=strPassWord
端接头
使用绘图对象:=0
而不是绘图对象:=false
为我工作