PC Review


Reply
Thread Tools Rate Thread

Create a user-defined function

 
 
Max
Guest
Posts: n/a
 
      23rd Jan 2008
Am more familiar with Access, but have created a function in XL07 as follows:

Public Function AgeIt()
Selection.NumberFormat = "0.00"
ActiveCell.FormulaR1C1 = "=(TODAY()-RC[-1])/365"
AgeIt = ActiveCell.FormulaR1C1
End Function

to calculate a person's age when the birthday is in the cell to the left.

When I select the function and insert it into a cell, I get =AgeIt() on the
formula bar, but #VALUE! in the cell.

What am I missing???

Thanks in advance for your help.
 
Reply With Quote
 
 
 
 
Bernard Liengme
Guest
Posts: n/a
 
      23rd Jan 2008
Functions cannot change the content or format of a cell
They can return a value
Try this

Public Function AgeIt(mycell)
AgeIt = (Date - mycell.Value) / 365
End Function

In the worksheet call this with, for example, =AGEIT(A1)

See also:
1) David McRitchie's site on "getting started" with VBA
http://www.mvps.org/dmcritchie/excel/getstarted.htm
2) The DATEDIF function
http://www.cpearson.com/excel/datedif.aspx
best wishes
--
Bernard V Liengme
Microsoft Excel MVP
www.stfx.ca/people/bliengme
remove caps from email

"Max" <(E-Mail Removed)> wrote in message
news:21B62A9D-01F0-48D8-8FB6-(E-Mail Removed)...
> Am more familiar with Access, but have created a function in XL07 as
> follows:
>
> Public Function AgeIt()
> Selection.NumberFormat = "0.00"
> ActiveCell.FormulaR1C1 = "=(TODAY()-RC[-1])/365"
> AgeIt = ActiveCell.FormulaR1C1
> End Function
>
> to calculate a person's age when the birthday is in the cell to the left.
>
> When I select the function and insert it into a cell, I get =AgeIt() on
> the
> formula bar, but #VALUE! in the cell.
>
> What am I missing???
>
> Thanks in advance for your help.



 
Reply With Quote
 
SeanC UK
Guest
Posts: n/a
 
      23rd Jan 2008
Hi Max,

Here is a ay to do it:

Public Function AgeIt(ByRef rngMyCell As Range) As Double
AgeIt = (Date - rngMyCell.Value) / 365
End Function

This way you must use the function as:

=AgiIt(B6)

where B6 is the cell with the date in.

Firstly, you need to return something to the cell calling the function,
which you do with your final line, although your previous lines should not
work (unless being called from within code that has not been initiated by a
worksheet function. The reason is that worksheet functions can only return
values to a cell, but the worksheet (including the contents of the cell - the
formula) cannot be changed by a worksheet function. Therefore, the formatting
of the cell and attempting to enter a formula in the cell (which is
attempting to replace your AgeIt formula with "=(TODAY()-RC[-1])/365") will
not work.

If the cells are already in General format then you should see the result as
"0.00" anyway.

I hope this helps,

Sean.
--
(please remember to click yes if replies you receive are helpful to you)


"Max" wrote:

> Am more familiar with Access, but have created a function in XL07 as follows:
>
> Public Function AgeIt()
> Selection.NumberFormat = "0.00"
> ActiveCell.FormulaR1C1 = "=(TODAY()-RC[-1])/365"
> AgeIt = ActiveCell.FormulaR1C1
> End Function
>
> to calculate a person's age when the birthday is in the cell to the left.
>
> When I select the function and insert it into a cell, I get =AgeIt() on the
> formula bar, but #VALUE! in the cell.
>
> What am I missing???
>
> Thanks in advance for your help.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create User Defined Function with VB.Net Troy Microsoft Excel Programming 7 17th Aug 2009 06:58 PM
Create user-defined function using Querytables david95554352 Microsoft Excel Worksheet Functions 0 19th Sep 2006 07:21 AM
How can I create a user defined function in excel? =?Utf-8?B?TWFydGluag==?= Microsoft Excel Misc 4 20th Aug 2005 06:11 PM
How to create User Defined Function Warwick Renshaw Microsoft Excel Programming 0 25th Jul 2003 07:15 AM
Re: Create help for user-defined function Tom Ogilvy Microsoft Excel Programming 0 12th Jul 2003 06:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:36 PM.