2017-07-25 166 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