Spin Button trigger issue

Showard27

Board Regular
Joined
Sep 5, 2005
Messages
155
I have a spin button on a user form (called PieSpinButton1)

in the userform initialization routine, I set max mins and default values depending on a number of conditions

Also in the form control I have this procedure which does "something" when the spin button changes

Code:
Private Sub YearSpinButton2_Change()
If YearSpinButton2.Value > Year(Now) Then
        YearSpinButton2.Value = Year(Now)
        Else: End If
                YearTextBox2.Value = YearSpinButton2.Value
                Worksheets("Constants").Range("B18").Value = YearSpinButton2.Value
                    Refresh_Chart
End Sub

however the initialization routine triggers the change procedure, which causes the change procedure to run. Can I stop this happening at userform initialization stage as it is causing problems

many thanks in advance
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Add a boolean variable at the top of the userform module, let's call it boolSetup.

At the start of the initialize event set it to True and at the end set it to False.

Now in the change event you can prevent the code running like this.
VBA Code:
Private Sub YearSpinButton2_Change()

    If boolSetup Then Exit Sub

    If YearSpinButton2.Value > Year(Now) Then
        YearSpinButton2.Value = Year(Now)
    End If
    YearTextBox2.Value = YearSpinButton2.Value
    Worksheets("Constants").Range("B18").Value = YearSpinButton2.Value
    Refresh_Chart

End Sub
 
Upvote 0
Hi,

Unlike workbook / worksheets events, there is no built-in routine that you can call to disable userform events. The workaround is to declare a Boolean variable at top of your forms code page to manage this for you

Rich (BB code):
Dim DisableEvents As Boolean

Private Sub YearSpinButton2_Change()

If DisableEvents Then Exit Sub

If YearSpinButton2.Value > Year(Now) Then
        YearSpinButton2.Value = Year(Now)
        Else: End If
                YearTextBox2.Value = YearSpinButton2.Value
                Worksheets("Constants").Range("B18").Value = YearSpinButton2.Value
                    Refresh_Chart
End Sub



Private Sub UserForm_Initialize()
    DisableEvents = True
   
    'your codes
   
   
    DisableEvents = False
End Sub

Hope helpful

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,213,538
Messages
6,114,217
Members
448,554
Latest member
Gleisner2

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