User defined function for range

Tanis

Active Member
Joined
Dec 8, 2002
Messages
305
I am trying to create a user defined function to calcutate the difference between a range of cells as in Max(a1:a21)-Min(a1:a21) I Thought I'd try to create a simple UDF for Area just to see how it works.

Function(Length as Double, Width as Double)
Area = Length * Width
End Function

Now this works, but only in the current workbook. If I try it in any other workbook, I get #NAME error. So, two questions. How would I code the Difference function and how can I get any workbook to see it?
Any help appreciated.

David
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Tanis,

It would appear that your function does not have a name, try this:

Code:
Function Area (Length as Double, Width as Double) 
Area = Length * Width 
End Function

I'm guessing that you wanted to call the function "Area", so that you could use the formula:

=Area(2,3)

or something like that.
 
Upvote 0
Hello David,

To make your function visible to all Excel files, put your code in the Personal.xls which is Excel's Global Macro file. To see it, click on Windows, UnHide and select it. Put your code with a Function name as Mark suggested in a standard module. Then save your code and hide it again. When you exit Excel it will ask if you want to save changes to Personal.xls. Say yes and you should be able to use your macro anywhere.
 
Upvote 0
Thanks for the reply guys. However it does not work. I have found the answer though and that is to create an add-in. The function is then available to any workbook. The second part of my post was not answered. What would be the code to find the difference between two range of cells. Excel does not seem to have a function for this. At the moment I use Max(CellRange)-Min(CellRange) which is a bit cumbersome. I would like to create my own function.

David
 
Upvote 0
Hi,

how about

Code:
Function Difference(CellRange As Range) As Double
Difference = Application.WorksheetFunction.Max(CellRange) - _
             Application.WorksheetFunction.Min(CellRange)
End Function

HTH

Alan
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,012
Members
449,060
Latest member
LinusJE

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