vba锁定/解锁选择宏

时间:2017-07-25 17:52:11

标签: vba selection locked

我想创建一个宏来锁定/解锁基于锁定范围属性的单元格,但是我无法触发语句的Case Null部分(前2个工作正常)

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

为什么不解雇?

谢谢!

1 个答案:

答案 0 :(得分: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