# User defined function for range

#### Tanis

##### Active Member
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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

#### Mark O'Brien

##### MrExcel MVP
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
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
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
Hi,

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

HTH

Alan

#### Tanis

##### Active Member
Thanks Alan. Just the trick.

David

Replies
0
Views
284
Replies
5
Views
206
Replies
4
Views
245
Replies
2
Views
221
Replies
3
Views
409

1,187,173
Messages
5,962,033
Members
438,576
Latest member
Cstewart29

### 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.

### Which adblocker are you using?

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

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