checkbox takes a LONG TIME to "register"

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
Hello all:

I have ten checkboxes. Some of them execute immediately. Some of them take good 10-20 seconds to see the change on the screen. this is one that takes a LONG time. Is there something that I am doing that could explain it?

Thanks,



Code:
Private Sub Checkbox4_change()
Dim i As Long
Dim c4string As String
     If CheckBox4.Value Then
         For i = 1 To 20
           Me.Controls("checkbox" & i).Value = False
         Next
         CheckBox4.Value = True
        
     
        TextBox7.Visible = True
        TextBox6.Visible = True
        TextBox5.Visible = True
        TextBox4.Visible = True
        
        TextBox1.Top = 42
        TextBox1.Height = 20
        TextBox4.Top = 70
        TextBox4.Height = 80
        TextBox5.Top = 160
        TextBox5.Height = 20
        'TextBox6.Top = 348
        'TextBox6.Height = height2add
        'TextBox7.Top = 456
        'TextBox7.Height = height2add
        'TextBox1.ZOrder
        
        
        
         With Me.TextBox1
         .Value = Range("Exodus4a").Value
         .Font.Bold = Range("exodus4a").Font.Bold
         .Font.Size = 20
         .ForeColor = RGB(255, 0, 0) ' RED
         End With
                  
         With Me.TextBox4
          c4string = Range("Exodus4B").Value & Range("exodus4c").Value & Range("exodus4d").Value
          c4string = c4string & Range("exodus4e").Value
          .Value = c4string
         .Font.Bold = Range("exodus4a").Font.Bold
         .Font.Size = 20
         .ForeColor = RGB(0, 0, 0)
         End With
      End If
         
End Sub


Gene Klein
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The code runs when you make a change to the checkbox. The code has within it lines that change Checkbox4 and other checkbox values. This will in turn trigger other Checkbox_Change event event procedures to run and also retrigger Checkbox4_Change to run again and again because the code toggles it's value.

This triggers other Checkbox_Change procedures.
Code:
         For i = 1 To 20
           Me.Controls("checkbox" & i).Value = False
         Next
         CheckBox4.Value = True

This, I think, may be why the code is taking so long to execute.

The code below will allow changes to the checkbox values without triggering their _Change procedures.
Code:
        [COLOR="Red"]Application.EnableEvents = False[/COLOR]
         For i = 1 To 20
           Me.Controls("checkbox" & i).Value = False
         Next
         CheckBox4.Value = True
        [COLOR="Red"]Application.EnableEvents = True[/COLOR]
 
Upvote 0
Application.EnableEvents has no effect on userform events. One must have one's own abort variable to stop cascading events.

Code:
Dim ufEventsDisabled As Boolean

Private Sub TextBox1_Change()
    If ufEventsDisabled then Exit Sub
    ' ...
    ufEventsDisabled = True
    TextBox1.Text = "something"
    ufEventsDisabled = False
    ' ...
End Sub
 
Upvote 0
Application.EnableEvents has no effect on userform events. One must have one's own abort variable to stop cascading events.

Code:
Dim ufEventsDisabled As Boolean
 
Private Sub TextBox1_Change()
    If ufEventsDisabled then Exit Sub
    ' ...
    ufEventsDisabled = True
    TextBox1.Text = "something"
    ufEventsDisabled = False
    ' ...
End Sub

I will try both and report back. The thing is, I believe I have that code even for the checkboxes that run quickly.

Gene Klein
 
Upvote 0
Application.EnableEvents has no effect on userform events. One must have one's own abort variable to stop cascading events.

Code:
Dim ufEventsDisabled As Boolean
 
Private Sub TextBox1_Change()
    If ufEventsDisabled then Exit Sub
    ' ...
    ufEventsDisabled = True
    TextBox1.Text = "something"
    ufEventsDisabled = False
    ' ...
End Sub

Mike - taking another look her - it seems to me that if I use your code, I will never get to run the code that I want to?

Gene Klein
 
Upvote 0
The code runs when you make a change to the checkbox. The code has within it lines that change Checkbox4 and other checkbox values. This will in turn trigger other Checkbox_Change event event procedures to run and also retrigger Checkbox4_Change to run again and again because the code toggles it's value.

This triggers other Checkbox_Change procedures.
Code:
         For i = 1 To 20
           Me.Controls("checkbox" & i).Value = False
         Next
         CheckBox4.Value = True

This, I think, may be why the code is taking so long to execute.

The code below will allow changes to the checkbox values without triggering their _Change procedures.
Code:
        [COLOR=red]Application.EnableEvents = False[/COLOR]
         For i = 1 To 20
           Me.Controls("checkbox" & i).Value = False
         Next
         CheckBox4.Value = True
        [COLOR=red]Application.EnableEvents = True[/COLOR]
Ok, I can report that, AFAICT, the Application.EnableEvents=True/False had not effect.

Gene Klein
 
Upvote 0
Application.EnableEvents has no effect on userform events. One must have one's own abort variable to stop cascading events.

Code:
Dim ufEventsDisabled As Boolean

Private Sub TextBox1_Change()
    If ufEventsDisabled then Exit Sub
    ' ...
    ufEventsDisabled = True
    TextBox1.Text = "something"
    ufEventsDisabled = False
    ' ...
End Sub

I stand corrected.

The ufEventsDisabled solution should work though.
 
Last edited:
Upvote 0
I stand corrected.

The ufEventsDisabled solution should work though.
Perhaps I don't follow. It seems that me that the ufEventsDisabled solution will just take me out of the macro without doing any of the relevant code. Or am I missing something here?

Gene Klein
 
Upvote 0
In my example,
The user types into the Text box (ufEventsDisabled=False at this time)
The TextBox_Change code runs.
The If ufEventsDisabled line does nothing
The first bit of '... code runs
ufEventsDisabled is set to True
The code sets TextBox1.Text to "something" causing the TextBox_Change to run again, before it is finished running the first time.
If ufEventsDisabled Exits this second (code induced) running of TextBox_Change
ufEventsDisabled is set to False
The first (user induced) running of TextBox_Change ends.
 
Upvote 0
In my example,
The user types into the Text box (ufEventsDisabled=False at this time)
The TextBox_Change code runs.
The If ufEventsDisabled line does nothing
The first bit of '... code runs
ufEventsDisabled is set to True
The code sets TextBox1.Text to "something" causing the TextBox_Change to run again, before it is finished running the first time.
If ufEventsDisabled Exits this second (code induced) running of TextBox_Change
ufEventsDisabled is set to False
The first (user induced) running of TextBox_Change ends.
Okay - but the user never types into the textbox in my example. Only the checkbox.

Gene Klein
 
Upvote 0

Forum statistics

Threads
1,222,013
Messages
6,163,390
Members
451,834
Latest member
tomtownson

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