OK。これは私をナットにしている。私はまだ永遠のループをしています。 私の以前の質問はこれまでに私を得ました>>>Using VBA code to modify OptionButton.value is activating the control.Click() SubVBAコードを抑制するcontrol.Click()サブ/イベントハンドラ
私はこれを試して試してみるためにいくつかのコード例を書き直しました。 私は、他の提案のとおり、 (例:Suppress events in UserForms)のようにブール値のハンドラを設定しました。 (SwitchOnEventResponder
)
クリックイベントが発生するコードに到達すると、ハンドラはTrue(これは_Click()サブをアクティブにする値の変更が可能)に戻ります。 OptionsDLG.OptionButton2.Value = True
ステートメントが実行されます。どうして?
モジュール内のコード:
Sub StartDLG()
Call changeframe(135)
'code to change the UserForm (to hide the text box)
Call PopulateText
OptionsDLG.Show
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then Cancel = True
End Sub
Sub PopulateText()
' Populates text box with initial text
OptionsDLG.TextBox1.Value = "Enter your msg here"
End Sub
Sub changeframe(FormH As String)
OptionsDLG.Height = FormH
OptionsDLG.Label1.Visible = False
OptionsDLG.TextBox1.Visible = False
End Sub
ユーザーフォーム "OptionsDLG" コード:
Public SwitchOnEventResponder As Boolean
Private Sub UserForm_Initialize()
Me.SwitchOnEventResponder = True
End Sub
Private Sub OptionButton2_Change()
MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder
If Me.SwitchOnEventResponder = False Then
Exit Sub
End If
End Sub
Private Sub OptionButton1_Click()
Unload Me
Call changeframe(135)
'code to change the UserForm
Call PopulateText
OptionsDLG.Show
End Sub
Private Sub OptionButton2_Click()
If Me.SwitchOnEventResponder = False Then
Exit Sub
End If
Unload Me
Call PopulateText
Me.SwitchOnEventResponder = False
MsgBox "Me.SwitchOnEventResponder = " & Me.SwitchOnEventResponder
OptionsDLG.OptionButton1.Value = False
OptionsDLG.OptionButton2.Value = True '<< This is where the loop starts. grrrr
Me.SwitchOnEventResponder = True
OptionsDLG.Show
End Sub
' This runs when the Ok button is clicked
Private Sub OK_Click()
If OptionButton1.Value = True Then MsgBox "HAPPY"
If OptionButton2.Value = True Then MsgBox "HAPPIER"
End Sub
' This runs when the cancel button is clicked
Private Sub Cancel_Click()
' Show the main dialogue
Stop
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then Cancel = True
End Sub
「public SwitchOnEventResponder As Boolean」は、userformではなく別のモジュールで宣言しようとしてください。私はこの方法とその作業を使用します。 – Sphinx
ちょうど好奇心から、なぜUserformでなく、モジュールの 'Private Sub Userform_QueryClose'ですか? – SilentRevolution
@Sphinx - userformから宣言をモジュールに移動しました。これでuserformは 'Me.SwitchOnEventResponder = True'ステートメントを認識しません – Joe90