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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Mark O'Brien

MrExcel MVP
Joined
Feb 15, 2002
Messages
3,530
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.
 

sbendbuckeye

Active Member
Joined
Nov 26, 2003
Messages
440
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.
 

Tanis

Active Member
Joined
Dec 8, 2002
Messages
305
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
 

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,518
Hi,

how about

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

HTH

Alan
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,091
Messages
5,768,039
Members
425,451
Latest member
JohnBrooksBiddle

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
Top