如何冻结和解冻 VBA 中的复选框

How to Freeze and Unfreeze a checkbox in VBA

我想在单击框后“冻结”单元格。我想在单元格中的声明不起作用。我总是收到运行时错误 1004,错误是 If 语句。这里的代码:

Private Sub CheckBox1_Click()
    
    If Range("H1").HasFormula Then 
        Range("H1").Value = Range("H1").Value
        Me.CheckBox1.Caption = "Unfreeze"
   Else
        Range("H1").Formula = "=IF(Sheet2!E1 = 12;Sheet2!E1;"")"//If value in E1 is 12, H1 = 12 otherwise the cell stays empty 
        Me.CheckBox1.Caption = "Freeze"
   End If
End Sub

请尝试下一种方式:

Private Sub CheckBox1_Click()
    
    If Range("H1").HasFormula Then 
        Range("H1").Value = Range("H1").Value
        Me.CheckBox1.Caption = "Unfreeze"
   Else
        Range("H1").Formula = "=IF(Sheet2!E1 = 12,Sheet2!E1,"""")" 
        Me.CheckBox1.Caption = "Freeze"
   End If
End Sub