如何停止userform button.initialize值更改运行button.click()

时间:2019-07-01 11:20:12

标签: excel vba

我想在初始化用户表单时将初始值/状态拉到按钮上。

我遇到的问题是:

"If setupws.Cells(question1row, currentsetupcol).Value = "1" Then
 Question1Button.Value = True"

然后跳到Question1Button_Click()子项中。

我如何才能在不跳入click子的情况下完成初始设置状态?

我希望click子项仅用于单击,而不是用户窗体的初始设置。

Private Sub UserForm_Initialize()
    Dim buildphasews As Worksheet
    Dim Leadprogws As Worksheet
    Dim inputWks As Worksheet
    Dim setupws As Worksheet

Set setupws = Worksheets("13 Table - BuildPhase Applic")
currentsetupcol = Application.WorksheetFunction.Match("Current Sheet 
Setup", setupws.Range("A1:AZ1"), 0)

  If setupws.Cells(question1row, currentsetupcol).Value = "1" Then
  Question1Button.Value = True
  Question1Button.Caption = "question 1 will be asked"
  MsgBox "will"
  Else
  Question1Button.Value = False
  Question1Button.Caption = "question 1 will not be asked"
  MsgBox "not"
  End If

 End Sub

 Private Sub Question1Button_Click() 

Select Case Question1Button.Value
    Case True
          'MsgBox "true"
        Question1Button.Caption = "Question 1 will be asked"

    Case False
        'MsgBox "false"
           Question1Button.Caption = "Question 1 will not be asked"

    End Select
 'Call PreviousSetup
End Sub

2 个答案:

答案 0 :(得分:0)

更改按钮的Value可以模拟实际单击按钮。如果我没有记错的话,再次将按钮的_Click()设置为Value之后引发了False事件,那么您检查按钮值的解决方案将无法正常工作。

尝试使用Checkbox

但是,如果只想使用按钮,则需要一个布尔值;像这样的东西:

' In your Userform Code
Option Explicit
Dim someBool as Boolean

Private Sub UserForm_Initialize()
    [...]
    ' Instead of Question1Button.Value = True you do
    someBool = True
    [...]
    ' And instead of Question1Button.Value = False you do
    Somebool = False
    [...]
End Sub

Private Sub Question1Button_Click() 
     Select Case SomeBool
     Case True
         Question1Button.Caption = "Question 1 will be asked"
     Case False
         Question1Button.Caption = "Question 1 will not be asked"
     End Select
     'Call PreviousSetup
End Sub

答案 1 :(得分:0)

我找到了application.enableevents函数。我将= false放在初始化子项中,将下面的内容放在button_click子项中。

Private Sub Question1Button_Click()
If Application.EnableEvents = False Then
Exit Sub
Else

Select Case Question1Button.Value
Case True
    'MsgBox "true"
    Question1Button.Caption = "Question 1 will be asked"
Case False
    'MsgBox "false"
    Question1Button.Caption = "Question 1 will not be asked"
End Select
End If

现在它可以满足我的所有需求。

仍然感谢您的回答。