Function code triggering

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Further to this thread of mine:


I have the following in Module1:

Rich (BB code):
Option Explicit

Function MyFn(firstval As Double, val As Double) As Double
    
    If firstval = 1 Then
    
        MyFn = val + 10
    
    Else
    
        MyFn = val + 20
    
    End If
    
End Function

In Sheet2 cell B2, I type:

Rich (BB code):
=myfn(Sheet1!A1, 10)

In Sheet1 cell A1, I have a value of 1.

Sheet2 cell B2 has the correct value of 30.

What I find strange is if I change the value in Sheet1 Cell A1, the code is triggered off.

For example if I change the value from 1 to 2, the code starts BUT if I simply press F2 in Sheet1 cell A1, the cell isn't triggered.

So my question is:

1. Why is the code triggered at all when I change the value in Sheet1 cell A1.

2. Why doesn't it change when I simply press F2?

Thanks
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
??

F2 (entering a cell for editing) doesn't trigger a calculation; hitting Enter does, even if the same value is re-entered.
 
Upvote 0
Sorry, I meant pressing F2, then Enter (so the value is entered but not changed) doesn't trigger the code.

Even if the value was 1 and I typed in 1, the code is still not triggered.
 
Last edited:
Upvote 0
Both of those should trigger it too.
 
Upvote 0
Both of those should trigger it too.
That's what I believed until two minutes ago. Reentering the same value in a single cell triggers the Change event, but not the Calculate event.

That's pretty smart.
 
Last edited:
Upvote 0
That's what I believed until two minutes ago. Reentering the same value in a single cell triggers the Change event, but not the Calculate event.

That's pretty smart.
Pretty smart? I don't know... if nothing actually changed in the cell, why is the Change event firing? I mean, won't the workbook end up in the same state afterwards as it was in before given the cell value did not change? What is the ultimate purpose is firing the Change event given that?
 
Last edited:
Upvote 0
The change event doesn't entail any overhead if there is no subscriber.
 
Upvote 0
The change event doesn't entail any overhead if there is no subscriber.
I am not sure what that means. My point, though, is this. Let's say you have a lengthy Change event procedure that takes a full minute to execute when cell A1 changes. What is the point in firing that code if you enter edit mode for cell A1 and then just hit the Enter key without changing the value? I mean, your code will tie everything up for a full minute and then return the worksheet to the same state it was in before you entered Edit mode... I don't see the point.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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