Why does thjs code trigger another

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,345
I have a userform with a textbox and a listbox. In the form's code module I have the following code:

Private Sub tbxAB_AfterUpdate()
shtCorrel3.Range("ValidMax").Calculate
End Sub

This code immediately triggers the procedure, "Private Sub lbxSource_Change()".
The calculation of the single cell {ValidMax) is unrelated to the Listbox source. Can anyone explain why it starts the second code and just as importantly how to stop it doing so?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I don't know the answer to your first question but as a first step you might try stopping it by coding:-
VBA Code:
Private Sub tbxAB_AfterUpdate()
Application.EnableEvents=False
shtCorrel3.Range("ValidMax").Calculate
Application.EnableEvents=True
End Sub

Just a suggestion – I haven't tested this.
 
Upvote 0
Is the listbox bound directly to a range on a worksheet using its Rowsource property?
 
Upvote 0
Hi Rory. Yes it is, but the cell being calculated does not affect the Rowsource cells. The cell (ValidMax) has no dependents. Are you suggesting that the code will trigger the ListBox whenever there is a calculation anywhere. If so, how can I stop it doing that? As a workaround I've made a Boolean Global variable which stops the ListBox code at the start. This prevents the unwanted effects that would occur if the whole code were allowed to run, but It's a pretty cluncky solution.
 
Upvote 0
What is the actual rowsource for the control?
 
Upvote 0
The RowSource is a named range "ListDs" which is defined as =OFFSET('BasicRiskModelv2-01d-6TT.xlsb'!D_LIst,1,0,'BasicRiskModelv2-01d-6TT.xlsb'!CtDs,1)
CtDs is the formula "=SUMPRODUCT(--(LEN(AF2:AF11)>0))". The cell I calculate, which causes all the trouble, is in N15 and does not appear to have anything to do with the named range. Does this help?
 
Upvote 0
That’s the issue. OFFSET is volatile so it recalculates whenever any open workbook does.
 
Upvote 0
Solution
Thanks Rory. That's really clever. I guess that means I have to go back to my clunky solution - Ugh! Nonetheless, I'm very grateful for your explanation.
 
Upvote 0
Since you're only loading one column, you could simply not use Rowsource at all. Just populate the listbox using its List property and update that whenever necessary.
 
Upvote 0

Forum statistics

Threads
1,216,033
Messages
6,128,427
Members
449,450
Latest member
gunars

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