cant understand this custom function

bigdan

Well-known Member
Joined
Oct 5, 2009
Messages
846
Office Version
  1. 2013
Platform
  1. Windows
I'm learning VBA at the moment but don't fully understand it yet. In this case below we're learning how to create a custom function. Can someone explain what's happening here? I'm not following.

I get what the function is trying to do. I'm good there. But it seems to me we're creating a function called IF_GREEN, and in that, if the cell color is green, then we're running the IF_GREEN function. How does that make sense? It seems like a self-referencing definition?



VBA If Green.PNG
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
A function is a procedure that (generally) returns some value. In order for it to be able to return a value, we insert its name on the left side of the assignment, and the calculated value on the right.

Artik
 
Upvote 0
It's not self-referencing. The function is called from Excel, like this:

ABCD
1
2100$100
3200$200
4300-
5400$400
6500-
7600$600
8700$700
9800-
10900$900
111000-
121100$1100
Sheet4
Cell Formulas
RangeFormula
D2:D12D2=IF_GREEN(C2,B2)

But it's not a great example. The Application.Volatile method means that the function recalculates whenever the worksheet recalculates. You don't want too much of this happening.

Also, simply changing a cell's colour will not trigger a recalculate, so if you start changing cell colours the results will be incorrect until a recalculation occurs.
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,337
Members
453,032
Latest member
Pauh

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