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
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,274
Office Version
  1. 365
Platform
  1. Windows
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,487
Office Version
  1. 2019
Platform
  1. Windows
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
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,859
Messages
5,627,293
Members
416,236
Latest member
Lynchbox

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
Top