User-defined functions

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have created a udf but I can't work out why it gets called more than once?

In Sheet1, I have values in cells B1, D1 and E1.

A1 contains a formula:

Code:
=D1+E1

and C1 contains my udf:

Code:
=MyCalc(B1,A1)

Here is the code:

Code:
Option Explicit

Sub Test()
  
    Sheet1.Cells(1, 1).FormulaR1C1 = "=RC[3]+RC[4]"
  
    Sheet1.Cells(1, 3).FormulaR1C1 = "=MyCalc(RC[-1],RC[-2])"
  
End Sub

Public Function MyCalc(Metric1 As Integer, Metric2 As Integer) As Integer

    MyCalc = Metric1 + Metric2
  
End Function

What I don't understand is why does this line of code:

Code:
    Sheet1.Cells(1, 1).FormulaR1C1 = "=RC[3]+RC[4]"

trigger the udf?

Is it because the udf takes two arguments, which are A1 and B1, so changing A1 and / or B1 triggers the udf?

Thanks
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Yes, assuming you already had that formula on the sheet.
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,258
Members
449,307
Latest member
Andile

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