简体   繁体   中英

vba locking / unlocking selection macro

I want to create a macro to lock / unlock cells based on Locked range property, but I am having trouble firing the Case Null section of the statement( The first 2 work fine)

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

Why is this not firing??

thanks!

Solution (if anyone is interested):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM