Отключить событие Text_Change VBA - PullRequest
0 голосов
/ 12 ноября 2018

Dears,

Есть ли способ отключить событие Textbox_change? Я пытаюсь

Application.EnableEvents = False 

но это не работает.

    If .txtHomeNumber.Value <> "" Then
        If IsNumeric(txtHomeNumber.Value) Then
            .txtHomeNumber.BackColor = RGB(255, 255, 255)
            .txtMobileNumber.BackColor = RGB(255, 255, 255)
            .txtParentsNumber.BackColor = RGB(255, 255, 255)
        Else: MsgBox "Please enter a valid Home Number in Contact Details section.", vbExclamation, "Contact Details"
            Application.EnableEvents = False
                .txtHomeNumber.Value = Left(.txtHomeNumber.Value, Len(.txtHomeNumber.Value) - 1)
            Application.EnableEvents = True
            Exit Sub
        End If
    End If

Ответы [ 2 ]

0 голосов
/ 13 ноября 2018

Обычно я определяю свой собственный логический тип, который выглядит следующим образом:

Public DisableEvents As Boolean

Private Sub SomeObject_Change()
    If Not DisableEvents Then
        DisableEvents = True
            ' do_stuff
        DisableEvents = False
    End If
End Sub

Это работает и предотвращает бесконечный цикл, который вы иначе создадите.

0 голосов
/ 13 ноября 2018

Этот код использует TextBox и CommandButton на Userform.
Я только что добавил кнопку, чтобы вы могли переключаться между включен и отключен .

Option Explicit

Private bEnabled As Boolean

Private Sub UserForm_Initialize()
    'Starts as FALSE so change to TRUE when form first opens.
    bEnabled = True
End Sub

Private Sub TextBox1_Change()

    If bEnabled Then
        MsgBox "Enabled"
'        If .txtHomeNumber.Value <> "" Then
'            If IsNumeric(txtHomeNumber.Value) Then
'                .txtHomeNumber.BackColor = RGB(255, 255, 255)
'                .txtMobileNumber.BackColor = RGB(255, 255, 255)
'                .txtParentsNumber.BackColor = RGB(255, 255, 255)
'            Else: MsgBox "Please enter a valid Home Number in Contact Details section.", vbExclamation, "Contact Details"
'                Application.EnableEvents = False
'                    .txtHomeNumber.Value = Left(.txtHomeNumber.Value, Len(.txtHomeNumber.Value) - 1)
'                Application.EnableEvents = True
'                Exit Sub
'            End If
'        End If
    End If

End Sub

Private Sub CommandButton1_Click()
    bEnabled = Not bEnabled
End Sub

Глядя на свой код - вы уверены, что не хотите, чтобы он был в событии AfterUpdate?

...