【问题标题】:vba locking / unlocking selection macrovba锁定/解锁选择宏
【发布时间】:2017-07-25 17:52:11
【问题描述】:

我想创建一个宏来根据 Locked range 属性锁定/解锁单元格,但我无法触发语句的 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

为什么没有触发?

谢谢!

【问题讨论】:

    标签: vba selection locked


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2022-08-22
      • 2016-09-26
      • 2013-03-22
      • 2010-12-07
      • 2022-11-16
      • 2018-02-10
      • 2014-05-14
      相关资源
      最近更新 更多