Question : event  code runs twice

Option Explicit
Dim LastTab As Integer

Private Sub TabStrip1_Change()
Application.EnableEvents = False
With TabStrip1
If .Value <> LastTab + 1 Then .Value = LastTab + 1
LastTab = .Value
End With
End Sub

Private Sub UserForm_Initialize()
With TabStrip1
Do Until .Tabs.Count >= 4
.Tabs.Add
Loop
LastTab = .Value
End With
End Sub


The above code makes sure there are at least 4 tabs on the tabstrip
Then if the active tab is changed
It makes sure its the "next tab", if it isn't then the correct or "next" tab is chosen

The issue is that the change tab event runs if the code changes the tab so the code runs twice if the program changes the tab.

This code & form are a much simplified version of the actual sheet and intended
only to demonstrate the issue. In real life that TabStrip1_Change does a bunch of other stuff

Short of adding a workaround If ... Then Exit Sub at the beginning of the code; how do I prevent the TabStrip1_Change code running when the tab is changed by the TabStrip1_Change code itself.
 
TabStrip1_Change Runs twice Issue
 

Answer : event  code runs twice

I personally set a variable indicating a working state, and check for it before running code:

eg dim initState as integer

inside _change() function check for init state before running code

inside initialise function set value before running and clear once completed.

Random Solutions  
 
programming4us programming4us