VBA jumping out of For Each loop to a different combo box subroutine

codeknot

New Member
Joined
May 23, 2018
Messages
2
Greetings, I'm seeking assistance to understand why the code below jumps out of the loop after assigning the cboSprint.Text value. Then resumes the loop at Exit For upon returning from cboSprint_Change() sub.

I've read other posts that suggest inserting Application.EnableEvents = False, or
Application.Calculation = xlCalculationManual statements, but neither failed to contain the code within CurSprintDate().

Private Sub CurSprintDate()
Dim rRow As Range
Dim iCount As Integer
iCount = 0
For Each rRow In rSprintDates 'Retrieved from rSprintDates (range $B$2:$B$28) public variable
iCount = iCount + 1
If Date <= rRow.Value Then
cboSprint.Text = cboSprint.List(iCount - 1) 'cboSprint.Text is assigned the correct value but then immediately jumps to cboSprint_Change() sub
Exit For 'the code resumes here after exiting cboSprint_Change() sub
End If
Next rRow
End Sub

Please help me understand what is causing this behavior.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Your changing the .Text argument of cboSprint triggers its Change event.
Userform events are not controled by Application.EnableEvents. One has to create one's own event disabling argument

Code:
' in userform

[COLOR="#FF0000"]Dim DisableMyEvents as Boolean[/COLOR]

Private Sub cboSprint_Change()
    [COLOR="#FF0000"]If DisableMyEvents Then Exit Sub[/COLOR]
    ' your code
End Sub

Private Sub CurSprintDate()
    Dim rRow As Range
    Dim iCount As Integer
    [COLOR="#FF0000"]If DisableMyEvents Then Exit Sub[/COLOR]
    iCount = 0
    For Each rRow In rSprintDates 'Retrieved from rSprintDates (range $B$2:$B$28) public variable
        iCount = iCount + 1
        If Date <= rRow.Value Then
           [COLOR="#FF0000"]DisableMyEvents = True[/COLOR]
           cboSprint.Text = cboSprint.List(iCount - 1)
           [COLOR="#FF0000"]DisableMyEvents = False[/COLOR]
           Exit For
        End If
    Next rRow
End Sub
 
Last edited:
Upvote 0
Thank you so much for your assistance! I implemented the code you provided and it worked great, plus you enlightened me as to why this was occurring. Happy Friday!
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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