2017-07-25 202 views
0

我想创建一个宏来锁定/基于锁定范围财产解开细胞,但我有麻烦射击声明(第2做工精细)VBA锁定/解锁选择宏

Sub Lockunlockselection() 

Dim c As Range 
Dim wb As Workbook 
Dim ws As Worksheet 
'Dim lck As String 
'Dim unlck As String 

Set wb = ActiveWorkbook 
Set ws = wb.ActiveSheet 
'lck = Empty 
'unlck = Empty 

Set c = selection 

Select Case c.Locked 
Case False 
c.Locked = True 
msgbox "Selection " & c.Address & " is now locked!", vbInformation, Date 
Case True 
c.Locked = False 
msgbox "Selection " & c.Address & " is now unlocked!", vbInformation, Date 
Case Null ' this would be if mix of locked and unlocked 
c.Locked = True 
msgbox "Mix of locked and unlocked cells!" & vbLf & vbLf & "Cells are all now locked!", vbInformation + vbExclamation, "Info.." 
End Select 

End Sub 
为例空区域

为什么这不会触发?

谢谢!

回答

0

解决方案(如果有人有兴趣):

Sub Lockunlockselection() 

Dim c As Range 
Dim wb As Workbook 
Dim ws As Worksheet 
'Dim lck As String 
'Dim unlck As String 

Set wb = ActiveWorkbook 
Set ws = wb.ActiveSheet 
'lck = Empty 
'unlck = Empty 

Set c = selection 

If IsNull(c.Locked) = True Then ' this would be if mix of locked and unlocked 
msgbox "Mix of locked and unlocked cells!" & vbLf & vbLf & "Cells are all now locked!", vbExclamation + vbMsgBoxSetForeground, "Info.." 
c.Locked = True 
Else 
    Select Case c.Locked 
    Case False 
    c.Locked = True 
    msgbox "Selection " & c.Address & " is now locked!", vbInformation, Date 
    Case True 
    c.Locked = False 
    msgbox "Selection " & c.Address & " is now unlocked!", vbInformation, Date 
    End Select 
End If 
End Sub