Disable _Change() upon initialization of userform

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
I have several textboxes in a userform with _change() events attached and the first thing that happens in the sub is a function that makes sure the user inputs a real number (as opposed to a letter) without any $ attached. When the userform is initialized, it is loaded with the saved data, which is formatted like $5,000, which trips the number-checking function, which gives me an message "please key in a number", when there is already a number in there.

So, my question is: Is it possible to temporarily deactivate the _change event ONLY upon initialization? Then after initialization, turn it on again?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Does "tabbing" or hitting "enter" or using the cursor to move around/through a pre-populated userform -- without changing anything in any textbox -- trigger a _change() event?
 
Upvote 0
I read the link on disabling events. Do i have this correct?

In my userform_Initialize, do I put:

enableevents=false for the first line of the sub
'do all of the userform initializing stuff here
enableevents=true for the last line of the initialization sub

and that is it?

Or, do I do the above AND put something like "If enable events =false, then exit" in each _change() sub at the beginning of the sub?

thanks for your help
 
Upvote 0
Sorry to keep bothering you. I want to turn off the _change() events before initializing, then initialize, then turn back on. this is the only time I want to turn them off and on. Here is the code i have, but it gives me an "object required" error.
Code:
Private Sub userform_Initialize()
UserForm.EnableEvents = False

TextBox1.Value = Format(Val(CStr(Sheet1.Range("a1").Value)), "###,###,##0")
TextBox2.Value = Format(Val(CStr(Sheet1.Range("a2").Value)), "0.0%")
TextBox3.Value = Format(Val(CStr(Sheet1.Range("a3").Value)), "0.0%")
TextBox4.Value = Format(Val(CStr(Sheet1.Range("a4").Value)), "0.0%")
TextBox5.Value = Format(Val(CStr(Sheet1.Range("a5").Value)), "###,###,##0")
UserForm.EnableEvents = True
End Sub
 
Upvote 0
...

Or, do I do the above AND put something like "If enable events =false, then exit" in each _change() sub at the beginning of the sub?

thanks for your help
Andrew is offline, but yes you need
Code:
If enable events =false, then exit sub
to be the start of the change event for every control that you write.
 
Upvote 0
I want to deactivate the _change() events, initialize the userform, then reactivate/turn on enableevents. I have the following code:
Code:
Private Sub userform_Initialize()
UserForm.EnableEvents = False

TextBox1.Value = Format(Val(CStr(Sheet1.Range("a1").Value)), "###,###,##0")
TextBox2.Value = Format(Val(CStr(Sheet1.Range("a2").Value)), "0.0%")
TextBox3.Value = Format(Val(CStr(Sheet1.Range("a3").Value)), "0.0%")
TextBox4.Value = Format(Val(CStr(Sheet1.Range("a4").Value)), "0.0%")
TextBox5.Value = Format(Val(CStr(Sheet1.Range("a5").Value)), "###,###,##0")
UserForm.EnableEvents=True
End Sub

But I get an "object required" error.
 
Last edited:
Upvote 0
Try this. The Dim EnableEvents line, placed before any procedure in the module, declares EnableEvents a private variable of the user form.
Note that the line
Code:
EnableEvents = True
must be the last line of the Intialize event, otherwise EnableEvents will be false and the event code will be blocked.

Code:
Dim EnableEvents As Boolean

Private Sub Userform_Initialize()
    EnableEvents = False
    TextBox1.Value = Format(Val(CStr(Sheet1.Range("a1").Value)), "###,###,##0")
    TextBox2.Value = Format(Val(CStr(Sheet1.Range("a2").Value)), "0.0%")
    TextBox3.Value = Format(Val(CStr(Sheet1.Range("a3").Value)), "0.0%")
    TextBox4.Value = Format(Val(CStr(Sheet1.Range("a4").Value)), "0.0%")
    TextBox5.Value = Format(Val(CStr(Sheet1.Range("a5").Value)), "###,###,##0")

    EnableEvents=True: Rem This line must be the last line in the Initialize event
End Sub

Private Sub TextBox1_Change()
    If EnableEvents = False Then Exit Sub
    ' code
End Sub

Private Sub TextBox2_Change()
    If EnableEvents = False Then Exit Sub
    ' code
End Sub

' etc

Private Sub TextBox5_Change()
    If EnableEvents = False Then Exit Sub
    ' code
End Sub
 
Upvote 0
1. If the initialization has occurred by the time the program gets to the last line, which turns the events back on, why do I need to check each sub after that? All I want is for the initialization to occur without tripping the _change events. After that, I want the _change events to trigger as normal.

2. If the above code (as written) does what I describe in questions #1, when do i turn it back on?
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top