Consulting

Results 1 to 19 of 19

Thread: Solved: User defined in-parameters in a function

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location

    Solved: User defined in-parameters in a function

    Hi,

    How do you create a function where you leave it to the user of the excel sheet to define which values the in-parameters should take by letting the user state the cell references (which holds these values)?

    For ex if I have created a very simple function which is called "mulitply" and which multiplies two values and I want to give the user the possibility to write (in the excelsheet) the formula:
    =multiply(A1;B1)
    so that this function goes into these cell references and look for values and then multiply the value in A1 with the value in B1, how do achieve this in my VBA custom Function code?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by danlu
    Hi,

    How do you create a function where you leave it to the user of the excel sheet to define which values the in-parameters should take by letting the user state the cell references (which holds these values)?

    For ex if I have created a very simple function which is called "mulitply" and which multiplies two values and I want to give the user the possibility to write (in the excelsheet) the formula:
    =multiply(A1;B1)
    so that this function goes into these cell references and look for values and then multiply the value in A1 with the value in B1, how do achieve this in my VBA custom Function code?
    If you created a function like so

    [vba]

    Function multiply(val1, val2)
    multiply = val1 * val2
    End Function
    [/vba]

    which will handle cells or constants.

  3. #3
    Hey, danlu, welcome to VBAX!

    I have a nitpicking comment:
    You wrote your function reference as
    =Multiply(A1;B1)

    It's probably a typo, but the semicolon should be a comma:
    =Multiply(A1,B1)

    Just in case you weren't aware.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Cyberdude
    Hey, danlu, welcome to VBAX!

    I have a nitpicking comment:
    You wrote your function reference as
    =Multiply(A1;B1)

    It's probably a typo, but the semicolon should be a comma:
    =Multiply(A1,B1)

    Just in case you weren't aware.
    Not necessarily. On the mainland continent (of Europe), the delimiter is a semi-colon, not comma. The comma is used as decimal separator in numbers, and so could throw the formula.

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Cyberdude
    It's probably a typo, but the semicolon should be a comma..
    Not in Sweden.

    If it could be a cell reference or a value directly given, I generally use the TypeName to check while adding some error handling in case textual characters are presented...

    [VBA]Function Multiply(val1 As Variant, val2 As Variant) As Variant
    Multiply = "ERROR!"
    On Error Resume Next
    If TypeName(val1) = "Range" Then
    If TypeName(val2) = "Range" Then
    Multiply = val1.Value * val2.Value
    Else
    Multiply = val1.Value * val2
    End If
    Else
    If TypeName(val2) = "Range" Then
    Multiply = val1 * val2.Value
    Else
    Multiply = val1 * val2
    End If
    End If
    End Function[/VBA]

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by firefytr
    Not in Sweden.
    I don't consider Sweden (or Norway, or Finland) part of mainland Europe. Mainland Europe is the main central block IMO, FGrance, Germany, Spain, Belgium, Luxembourg, Liechenstein, Italy, etc.

    Sweden is odd, they even drove on the left once.

  7. #7
    That's the place where they always misspell the word "color".

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    That's not the only place, my friend!

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Cyberdude
    That's the place where they always misspell the word "color".
    In Sweden they wouldn't spell colour, they have their own word.

    You are in the place where they mis-spell colour, and where they mis-use momentarily, and where .... need I go on. Remember, it is called the English language because it originated in England.

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    English, blah, blah, England, blah, blah. Err.. I think it's.. Yadda, yadda, yadda..

  11. #11
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location

    Format of in-parameters

    Hi,

    Is val1 and val2 some sort of predefined parameter (variable) or how can VBA treat both cells and constants (usually when you define variables you assign them some sort of format)?


    Quote Originally Posted by xld
    If you created a function like so

    [vba]

    Function multiply(val1, val2)
    multiply = val1 * val2
    End Function
    [/vba]

    which will handle cells or constants.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because they default to variant type, and they will take the sub-type of the parameter passed to it.

  13. #13
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location

    Thanks

    Hi,

    Thanks very much, that made things a lot clearer!


    Quote Originally Posted by xld
    Because they default to variant type, and they will take the sub-type of the parameter passed to it.

  14. #14
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Quote Originally Posted by xld
    Sweden is odd, they even drove on the left once.
    What, before they came to their senses? Better take care down here before stepping out into traffic! Colour me embarrassed - next thing you know I'll be typing dates in dd/mm/yy format ...

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by geekgirlau
    What, before they came to their senses? Better take care down here before stepping out into traffic! Colour me embarrassed - next thing you know I'll be typing dates in dd/mm/yy format ...

    http://www.hyperdictionary.com/search.aspx?define=irony

  16. #16
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by firefytr
    Not in Sweden.
    The list separator character is set under Regional Options and the default for Sweden is semicolon.

    Quote Originally Posted by xld
    Sweden is odd, they even drove on the left once.
    But they had left-hand drive cars even when they drove on the left. Odd indeed!
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  17. #17
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Actually I get the irony xld, it's just that down here in Aus we have our hands full driving on the left and putting the "u" in colour, so don't have much opportunity to practice it ourselves ... (oh, and the image refers to those of us who insist on driving on the wrong side of the road when the rest of world does the reverse!)

  18. #18
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by geekgirlau
    Actually I get the irony xld, it's just that down here in Aus we have our hands full driving on the left and putting the "u" in colour, so don't have much opportunity to practice it ourselves ... (oh, and the image refers to those of us who insist on driving on the wrong side of the road when the rest of world does the reverse!)
    So do we here in England, all those things inclduing the date, which is why I said that.

  19. #19
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by xld
    So do we here in England, all those things including the date, which is why I said that.
    ...and North of the border too.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •