【问题标题】:Unchecked checkbox value does not result in zero value未选中的复选框值不会导致零值
【发布时间】:2016-03-28 23:25:35
【问题描述】:

在 Excel 2010、VBA 中,当未选中该复选框时,代码不应运行。但是,当未选中该复选框时它正在运行。我错过了什么?

编辑:未选中的复选框会在“立即”窗口中产生“-4146”。所以我将IF 条件更改为<> 1。未选中的复选框不应该等于零吗?

编辑 2:我忘了说,这是一个 FORM CONTROL 复选框。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'delete range on the active row from column I to column M and shift up
'I=9, M = 13
Dim delAnswer As VbMsgBoxResult
Const WS_Range As String = "I:M"

'Checks to see if user wants macro turned on
If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = 0 Then Exit Sub
On Error GoTo ws_exit
Application.EnableEvents = False

'Check to see if user is in applicable range for macro to run
If Not Intersect(ActiveCell, Me.Range(WS_Range)) Is Nothing Then

'Ask user to confirm running macro
With Target
aR = ActiveCell.Row
delAnswer = MsgBox("Delete I" & aR & ":M" & aR & "?", vbYesNo)

'Macro - deletes range of cells on the active row
If delAnswer = vbYes Then
Range("I" & ActiveCell.Row).Resize(, 5).Delete Shift:=xlUp

Else: GoTo ws_exit
End If
End With
End If

ws_exit:
Application.EnableEvents = True
'Debug.Print Selection.Address(ReferenceStyle:=xlA1, RowAbsolute:=False, ColumnAbsolute:=False)
'Debug.Print "ar is " & aR
End Sub

【问题讨论】:

  • 这个怎么样:If Me.CheckBox4.Value = 0 Then ...但请确保您拥有正确的复选框对象名称(请参阅“属性”框)。换句话说,它的 Caption Check Box 4 可能与它的 object name 不同。
  • 当我尝试这个 `Me.Checkbox4.Value 时,我收到错误“找不到方法或数据成员。”
  • 是的,请看我的回答。我在您编辑帖子之前发布了该评论,以说明它是一个表单控件。
  • 表单复选框的值为xlOnxlMixedxlOff

标签: excel if-statement checkbox vba


【解决方案1】:

改变

If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = 0

If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = -4146

-4146 是未选中的表单控件复选框的数字等效项。要测试复选框是否确实被选中,请使用= 1 而不是= -4146

【讨论】:

    【解决方案2】:

    我会通过以下方式确定表单复选框是否未选中:

    If Not ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1 Then
    
        MsgBox ("unchecked")
    
    End If
    

    我参考了以下关于如何检测 Form 或 ActiveX 控件的检查值的答案:https://stackoverflow.com/a/11991419/5692872

    【讨论】:

    • 这不是一个可接受的答案。如果您想链接到上一个问题,请将其放在 OP 的 cmets 中。 (然后将其删除作为答案)。
    【解决方案3】:

    我发现这两个都可以通过更改:
    If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = 0
    至:
    If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value <> 1

    If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = xloff

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2014-07-20
      • 2013-08-25
      相关资源
      最近更新 更多