Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default UDF, Category, Data Type, Format, Using

I've written a UDF that calculates the P&I payment for a loan.

Public Function PandI(Principal As Long, AIR As Single,
Years As Integer, Optional n As Integer = 12) As Currency
PandI = (Principal * AIR / 100 / n * (1 + AIR / 100 / n)
^ (n * Years)) / ((1 + AIR / 100 / n) ^ (n * Years) - 1)
End Function
Whe
Principal might be: 250000
AIR (Annual Interest Rate) might be: 4.875
Years might be: 30
n (number of payments per year) is usually: 12
The result will be 1323.0206

I thought giving it data type currency would make it display as
$1,323.02 but that doesn't happen unless I format the cell.

The function is in my Personal.xls. It appears in the Paste
Function Dialog (Function Wizard) in Category:All as
Personal.xls!PandI.
Following instructions from a book, I put the following
procedure in a module in Personal.xls.
Public Sub Auto_Open()
Application.MacroOptions macro:="PandI", Category:=1
End Sub
but I get an error and it doesn't work.
Also, is it possible to make it so that the function can be
used on the worksheet by just typing "=pandi"?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default UDF, Category, Data Type, Format, Using

John,

Allocating the category as you show works okay for me. What is the error
text?

Also, you have to include the file name if you put it in Personal.xls. To
avoid this, you will need an addin.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"John Pierce" wrote in message
m...
I've written a UDF that calculates the P&I payment for a loan.

Public Function PandI(Principal As Long, AIR As Single,
Years As Integer, Optional n As Integer = 12) As Currency
PandI = (Principal * AIR / 100 / n * (1 + AIR / 100 / n)
^ (n * Years)) / ((1 + AIR / 100 / n) ^ (n * Years) - 1)
End Function
Whe
Principal might be: 250000
AIR (Annual Interest Rate) might be: 4.875
Years might be: 30
n (number of payments per year) is usually: 12
The result will be 1323.0206

I thought giving it data type currency would make it display as
$1,323.02 but that doesn't happen unless I format the cell.

The function is in my Personal.xls. It appears in the Paste
Function Dialog (Function Wizard) in Category:All as
Personal.xls!PandI.
Following instructions from a book, I put the following
procedure in a module in Personal.xls.
Public Sub Auto_Open()
Application.MacroOptions macro:="PandI", Category:=1
End Sub
but I get an error and it doesn't work.
Also, is it possible to make it so that the function can be
used on the worksheet by just typing "=pandi"?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default UDF, Category, Data Type, Format, Using

Bob,
Thanks for your response.
I was wondering where you saw my post. I can't find it on
microsoft.public.excel.programming which is where I thought
I had posted it. Anyway, I can let go of the part about
making the function available in the Financial category.
What matters more to me is the way it displays. The result
shows up as 1323.0206 but I want $1,323.02 without having
to format the cell. I thought making the function's
data type currency would do that.

JohnSCPierce

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default UDF, Category, Data Type, Format, Using

John,

You are getting the currency worksheet format confused with the currency
data type. The former is in the form £ #,##0.00, whereas this is how defines
the currency data type '... Currency variables are stored as 64-bit (8-byte)
numbers in an integer format, scaled by 10,000 to give a fixed-point number
with 15 digits to the left of the decimal point and 4 digits to the right.
This representation provides a range of -922,337,203,685,477.5808 to
922,337,203,685,477.5807. ...'.

If you don't mind the result as a string (that is. no math on it), you could
try

Public Function PandI(Principal As Long, _
AIR As Single, _
Years As Integer, _
Optional n As Integer = 12) As String
PandI = Format((Principal * AIR / 100 / n * (1 + AIR / 100 / n) _
^ (n * Years)) / ((1 + AIR / 100 / n) ^ (n * Years) - 1), "$#,##0.00")
End FunctionPublic Function PandI(Principal As Long, _
AIR As Single, _
Years As Integer, _
Optional n As Integer = 12) As String
PandI = Format((Principal * AIR / 100 / n * (1 + AIR / 100 / n) _
^ (n * Years)) / ((1 + AIR / 100 / n) ^ (n * Years) - 1), "$#,##0.00")
End Function

otherwise you need to format the cell as currency.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"John Pierce" wrote in message
...
Bob,
Thanks for your response.
I was wondering where you saw my post. I can't find it on
microsoft.public.excel.programming which is where I thought
I had posted it. Anyway, I can let go of the part about
making the function available in the Financial category.
What matters more to me is the way it displays. The result
shows up as 1323.0206 but I want $1,323.02 without having
to format the cell. I thought making the function's
data type currency would do that.

JohnSCPierce

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 623
Default UDF, Category, Data Type, Format, Using

Why use a UDF? Why not just use the PMT function?

=PMT(4.875%/12,30*12,-250000) will return 1323.02

If you don't want to format the cell (for whatever reason), you can use the
Text function to generate the string, eg

=TEXT(PMT(....),"$#,###.00") will return $1,323.02

--
Regards,
Fred
Please reply to newsgroup, not e-mail


"John Pierce" wrote in message
m...
I've written a UDF that calculates the P&I payment for a loan.

Public Function PandI(Principal As Long, AIR As Single,
Years As Integer, Optional n As Integer = 12) As Currency
PandI = (Principal * AIR / 100 / n * (1 + AIR / 100 / n)
^ (n * Years)) / ((1 + AIR / 100 / n) ^ (n * Years) - 1)
End Function
Whe
Principal might be: 250000
AIR (Annual Interest Rate) might be: 4.875
Years might be: 30
n (number of payments per year) is usually: 12
The result will be 1323.0206

I thought giving it data type currency would make it display as
$1,323.02 but that doesn't happen unless I format the cell.

The function is in my Personal.xls. It appears in the Paste
Function Dialog (Function Wizard) in Category:All as
Personal.xls!PandI.
Following instructions from a book, I put the following
procedure in a module in Personal.xls.
Public Sub Auto_Open()
Application.MacroOptions macro:="PandI", Category:=1
End Sub
but I get an error and it doesn't work.
Also, is it possible to make it so that the function can be
used on the worksheet by just typing "=pandi"?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
A SUB CATEGORY USING DATA VALIDATION? ALRDY USNG DV FOR CATEGORY sameer Excel Discussion (Misc queries) 2 January 11th 10 12:55 PM
find location of maximum and get the category type Pablo Excel Discussion (Misc queries) 4 September 27th 08 05:16 PM
Format Category Labels Jampot Charts and Charting in Excel 2 September 19th 07 06:40 PM
Format a column wihtout using "Format-Cells-Number-category-etc" serve Excel Worksheet Functions 4 February 24th 06 12:54 AM
Adding new 'Type' to Format->Number->Time->Type Chip Pearson Excel Discussion (Misc queries) 5 September 26th 05 08:45 PM


All times are GMT +1. The time now is 07:29 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"