跳过复选框单击()函数[VBA]

时间:2016-03-17 11:36:48

标签: vba excel-vba checkbox excel

我有用户表单,用于显示项目的状态。这些项目经历了3个阶段的过程。对于每个流程,都有一个复选框,可以在项目通过阶段后进行检查。点击后,会向用户显示"您确定吗?"提示。如果用户选择"是" (vbYes)进程在SQL表中标记为已完成。

当我想展示已经过一个或多个阶段的项目时,会出现问题。初始化表单时,我将相关阶段的复选框的值设置为" True"。但是,这会激活复选框的单击功能,这不是预期的。有没有办法跳过这个?

代码示例(非实际代码)

Private Sub UserForm_Initialize()
    'Check check boxes, where stages are completed
    If stage_1_completed Then
        chkStage1.value = True
    End If
    If stage_2_completed Then
        chkStage2.value = True
    End If
    If stage_3_completed Then
        chkStage1.value = True
    End If

End Sub

'This part should only run, if the check box is clicked manually
Private Sub chkStage1_Click()
    If chkStage1.value = True Then
        'Prompt user if sure?
        promptAnswer = MsgBox("Are you sure?", vbYesNo, "Continue?")

        'Mark stage 1 as processed if yes
        If promptAnswer = vbYes Then
            set_stage_completed (1)
        End If

    End If
End Sub

1 个答案:

答案 0 :(得分:1)

一种方法是使用变量来存储状态:

Public EnableEvents As Boolean

Private Sub UserForm_Initialize()
    Me.EnableEvents = False

    ''
    ' initialize form here
    ''

    Me.EnableEvents = True
End Sub

Private Sub chkStage1_Click()
    If Not Me.EnableEvents Then Exit Sub

    ''
    ' handle event here
    ''
End Sub