Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing parameters to UDF

Hi,

There is a silly issue I got stuck for a while.

I used to write mostly procedures rather than functions. In order t
make them clear and easy to read when I called them I have formatte
the text like that:

procedure1 _
parameter1:=value1, _
parameter2:=value2, _
parameter3:=value3, etc.

now I wanted to do the same with UDF, but it doesn't work. The workin
format is:

function1(value1, value2, value3, etc)

but it is harder to read afterwards.

It seems to me silly, but there is no direct example on MSDN or thi
forum, so I decided to ask.


Thanks in advance,


Mbobr

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Passing parameters to UDF

Hi

How to code and what's easy to read is just a question of style, habit and taste.
Personally I use Call with all my procedures, so all passed variables are embraced with
parentheses. But no matter which choice, all variables and functions should be declared
with sesible data types and named reasonably. Here's a sample of the different ways I know
to pass stuff to a Sub and a Function. To your question, yes I think you must put
parentheses around variables to functions, and I think you should do that to subs too.

Sub test()
Dim dtBorn As Date
Dim Msg As String
dtBorn = DateSerial(1956, 12, 31)

Msg = BirthdayAlert(strFirstName:="Joe", _
strLastName:="Doe", _
dtDayOfBirth:=dtBorn)
MsgBox Msg

' I use this style for functions:
Msg = BirthdayAlert("Joe", _
"Doe", _
dtBorn)
MsgBox Msg

ShowBornInfo "Joe", _
"Doe", _
dtBorn

ShowBornInfo _
strFirstName:="Joe", _
strLastName:="Doe", _
dtDayOfBirth:=dtBorn

Call ShowBornInfo( _
strFirstName:="Joe", _
strLastName:="Doe", _
dtDayOfBirth:=dtBorn)

' I use this style for Subs:
Call ShowBornInfo("Joe", _
"Doe", _
dtBorn)

End Sub

Function BirthdayAlert( _
strFirstName As String, _
strLastName As String, _
dtDayOfBirth As Date) As String

Dim dtNext As Date
dtNext = DateSerial(Year(Date), _
Month(dtDayOfBirth), _
Day(dtDayOfBirth))

If dtNext = Date Then
BirthdayAlert = strFirstName & " " & _
strLastName & " is " & _
Year(Date) - Year(dtDayOfBirth) & _
" today ! Hooray !"
Else
If dtNext < Date Then _
dtNext = DateSerial(Year(Date) + 1, _
Month(dtDayOfBirth), _
Day(dtDayOfBirth))
BirthdayAlert = strFirstName & " " & _
strLastName & " is " & _
Year(Date) - Year(dtDayOfBirth) & _
" on " & Format(dtNext, "dddd mmmm d. yyyy")
End If
End Function

Sub ShowBornInfo( _
strFirstName As String, _
strLastName As String, _
dtDayOfBirth As Date)
MsgBox strFirstName & " " & _
strLastName & " is born " & _
Format(dtDayOfBirth, "dddd mmmm d. yyyy")
End Sub

--
HTH. Best wishes Harald
Followup to newsgroup only please.

"mbobro" wrote in message
...
Hi,

There is a silly issue I got stuck for a while.

I used to write mostly procedures rather than functions. In order to
make them clear and easy to read when I called them I have formatted
the text like that:

procedure1 _
parameter1:=value1, _
parameter2:=value2, _
parameter3:=value3, etc.

now I wanted to do the same with UDF, but it doesn't work. The working
format is:

function1( value1, value2, value3, etc)

but it is harder to read afterwards.

It seems to me silly, but there is no direct example on MSDN or this
forum, so I decided to ask.


Thanks in advance,


Mbobro


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Passing parameters to UDF

What doesn't work?

A UDF stands for user defined function and is a function that can be
used from an XL worksheet.

It is not possible that the procedure you are calling is from an XL
worksheet.

So, I am not sure what you are trying to do. If you are trying to use
a function in a worksheet, yes, you cannot specify named arguments.
They all have to be specified by position. On the other hand, from
within code, you can use something like:

--
Regards,

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

In article ,
says...
Hi,

There is a silly issue I got stuck for a while.

I used to write mostly procedures rather than functions. In order to
make them clear and easy to read when I called them I have formatted
the text like that:

procedure1 _
parameter1:=value1, _
parameter2:=value2, _
parameter3:=value3, etc.

now I wanted to do the same with UDF, but it doesn't work. The working
format is:

function1(value1, value2, value3, etc)

but it is harder to read afterwards.

It seems to me silly, but there is no direct example on MSDN or this
forum, so I decided to ask.


Thanks in advance,


Mbobro


---
Message posted from
http://www.ExcelForum.com/


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Passing parameters to UDF

What doesn't work?

A UDF stands for user defined function and is a function that can be
used from an XL worksheet.

It is not possible that the procedure you are calling is from an XL
worksheet.

So, I am not sure what you are trying to do. If you are trying to use
a function in a worksheet, yes, you cannot specify named arguments.
They all have to be specified by position. On the other hand, from
within code, you can use something like:

Option Explicit

Function aFunc(x, y)
aFunc = x ^ 2 + y ^ 3
End Function
Sub testIt()
MsgBox aFunc(y:=2, x:=3)
End Sub

--
Regards,

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

In article ,
says...
Hi,

There is a silly issue I got stuck for a while.

I used to write mostly procedures rather than functions. In order to
make them clear and easy to read when I called them I have formatted
the text like that:

procedure1 _
parameter1:=value1, _
parameter2:=value2, _
parameter3:=value3, etc.

now I wanted to do the same with UDF, but it doesn't work. The working
format is:

function1(value1, value2, value3, etc)

but it is harder to read afterwards.

It seems to me silly, but there is no direct example on MSDN or this
forum, so I decided to ask.


Thanks in advance,


Mbobro


---
Message posted from
http://www.ExcelForum.com/


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Passing parameters to UDF

You can't have named parameters in an Excel function.

What you can do is use the tooltip, or the function wizard.
Also, you could start typing the function and press CTRL+SHIFT+A.
eg. Type =VLOOKUP( then press those keys for the names of the arguments.


"mbobro" wrote in message
...
Hi,

There is a silly issue I got stuck for a while.

I used to write mostly procedures rather than functions. In order to
make them clear and easy to read when I called them I have formatted
the text like that:

procedure1 _
parameter1:=value1, _
parameter2:=value2, _
parameter3:=value3, etc.

now I wanted to do the same with UDF, but it doesn't work. The working
format is:

function1(value1, value2, value3, etc)

but it is harder to read afterwards.

It seems to me silly, but there is no direct example on MSDN or this
forum, so I decided to ask.


Thanks in advance,


Mbobro


---
Message posted from http://www.ExcelForum.com/





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing parameters to UDF

After a small Christmas break I'm back to my code.

Thanks everyone for help, in a way a kind of Christmas gift for me!

Michal


---
Message posted from http://www.ExcelForum.com/

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
Passing several parameters to OnAction property. LABKHAND Excel Discussion (Misc queries) 1 April 24th 09 08:02 PM
Passing parameters Cel Ref) to Sum function Shamshad Butt Excel Discussion (Misc queries) 4 October 26th 05 10:46 AM
Passing Parameters to Userdefined Functions Peter M[_3_] Excel Programming 3 December 13th 03 07:56 PM
PASSING an array to a sub in VBA Wombat[_2_] Excel Programming 2 November 30th 03 11:48 AM
Passing Parameters through OnAction Mark Bigelow Excel Programming 3 September 10th 03 12:53 AM


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

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

About Us

"It's about Microsoft Excel"