Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Martinj
 
Posts: n/a
Default How can I create a user defined function in excel?

If I have a equation eg: =(A1+B1*if(t10=TRUE;1;-1)), how can I define it to
a Myfunction (A1;B1;T10)?

  #2   Report Post  
Bernard Liengme
 
Posts: n/a
Default

Use Alt+F11 to open VB Editor
Use command Insert | Module
Copy this to the newly open module sheet
Function Myfunction(x, y, z)
If z Then
multi = 1
Else
multi = -1
End If
Myfunction = x + y * multi
End Function
Return to Excel and type in any cell =Myfunction(A1,B1, T10)

But why not use
Function Myfunction(x, y, z)
If z Then
Myfunction = x + y
Else
Myfunction = x - y
End If
End Function

best wishes

--
Bernard V Liengme
www.stfx.ca/people/bliengme
remove caps from email

"Martinj" wrote in message
...
If I have a equation eg: =(A1+B1*if(t10=TRUE;1;-1)), how can I define it
to
a Myfunction (A1;B1;T10)?



  #3   Report Post  
Tushar Mehta
 
Posts: n/a
Default

Or what I would use:

MyFunc = x + IIf(z, y, -y)
MyFunc = x + y * IIf(z, 1, -1)

To the point, no code duplication, and easier to maintain.

It would be even more useful if VB terminated its evaluation as soon as
the result was determined.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Use Alt+F11 to open VB Editor
Use command Insert | Module
Copy this to the newly open module sheet
Function Myfunction(x, y, z)
If z Then
multi = 1
Else
multi = -1
End If
Myfunction = x + y * multi
End Function
Return to Excel and type in any cell =Myfunction(A1,B1, T10)

But why not use
Function Myfunction(x, y, z)
If z Then
Myfunction = x + y
Else
Myfunction = x - y
End If
End Function

best wishes


  #4   Report Post  
Dnereb
 
Posts: n/a
Default


Some further comment function where made to return a value
subs don't so write subs like:
Code:
--------------------
Sub MySub(X,Y,Z)

workbooks(Z).sheets(1).cells(X,Y).activate

end sub
--------------------


and function


Code:
--------------------
function Smallest (A,B,optional C) as long

if A < B then
smallest = A
else
Smallest = B
end if

if not ismissing(C) then
if C < Smallest then Smallest = C
end if

end function
--------------------
The function will return the smallest of the 2 or three numbers or cell
value's


--
Dnereb
------------------------------------------------------------------------
Dnereb's Profile: http://www.excelforum.com/member.php...o&userid=26182
View this thread: http://www.excelforum.com/showthread...hreadid=397300

  #5   Report Post  
Bernard Liengme
 
Posts: n/a
Default

I have never liked the IIF construct!
--
Bernard V Liengme
www.stfx.ca/people/bliengme
remove caps from email

"Tushar Mehta" wrote in message
om...
Or what I would use:

MyFunc = x + IIf(z, y, -y)
MyFunc = x + y * IIf(z, 1, -1)

To the point, no code duplication, and easier to maintain.

It would be even more useful if VB terminated its evaluation as soon as
the result was determined.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Use Alt+F11 to open VB Editor
Use command Insert | Module
Copy this to the newly open module sheet
Function Myfunction(x, y, z)
If z Then
multi = 1
Else
multi = -1
End If
Myfunction = x + y * multi
End Function
Return to Excel and type in any cell =Myfunction(A1,B1, T10)

But why not use
Function Myfunction(x, y, z)
If z Then
Myfunction = x + y
Else
Myfunction = x - y
End If
End Function

best wishes




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
How do I create a ratio function in Excel? Michele Excel Worksheet Functions 2 May 24th 05 05:47 PM
Excel user management CTE Excel Discussion (Misc queries) 1 February 22nd 05 12:12 PM
User defined charts- font size too small Bill B Charts and Charting in Excel 1 December 30th 04 06:23 PM
HOW CAN I GET OFFICE 2003 EXCEL BASIC TO NEST FUNCTIONS LIKE EXCE. Robert AS Excel Worksheet Functions 4 December 2nd 04 10:49 AM
Statistical Excel Function Question within Excel 2000... Drew H Excel Worksheet Functions 3 October 31st 04 06:55 PM


All times are GMT +1. The time now is 11:45 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"