When to use WorksheetFunction and when not?

JohnKauffman

New Member
Joined
Nov 1, 2012
Messages
36
When using a function within my code I can not figure out the logic of when to use WorksheetFunction and when not.

As test shows below, Sum() needs WorksheetFunction but ABS() fails with WorksheetFunction.
I usually figure it out by trial and error, but would like to understand the logic. I get a clue from intellisense options.
Are there two "classes" or groups of functions

ActiveCell.Value = WorksheetFunction.Sum(1, 2) 'Works
ActiveCell.Value = Sum(1, 2) 'Fails
ActiveCell.Value = WorksheetFunction.Abs(-5) 'Fails
ActiveCell.Value = Abs(-5) 'Works
 
You use WorksheetFunction when the function is a member of the WorksheetFunction class. Generaly that is when there is no VBA equivalent.

There is no VBA function Sum, there is a WorksheetFunction.Sum.
There is a VBA function Abs, there is no WorksheetFunction.Abs
There is a VBA function Replace, there is also a WorksheetFunction.Replace, but they do different things.

To get a list of all the WorksheetFunctions, look in the Object Browser.
 
Upvote 0

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