+ Reply to Thread
Results 1 to 22 of 22

function argument variables

  1. #1
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    function argument variables

    im writing a function for excel, and i have three arguments, two are integer variables, and the last is a string variable used to store a mathematical expression which needs to be able to use one of the variables previously mentioned to do math. is there a way to do this? and i'm pretty sure im using the wrong variable type for the expression, which is the correct one?

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: function argument variables

    Look into the Worksheet. (or Application.) Evaluate method.
    Last edited by leelnich; 03-05-2018 at 01:45 PM.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  3. #3
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    Re: function argument variables

    My code looks like this (it doesn't work) :
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    Re: function argument variables

    here is my workbook.
    Attached Files Attached Files

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: function argument variables

    You aren't actually passing an equation, but the result of it, so this should suffice
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    Re: function argument variables

    I am trying to have an incremented input (i) be run through an expression (Equation) (n) times then have all (n) outputs be added together.
    it should be something like this:
    =SIGMA(9,1,2*i)
    ^^type in cell^^
    (2*1)+(2*2)+(2*3)+(2*4)+(2*5)+(2*6)+(2*7)+(2*8)+(2*9)
    ^^Calculation Steps^^
    =90
    ^^Returns^^
    Last edited by Batnoob; 03-05-2018 at 11:54 PM. Reason: Clarification

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: function argument variables

    Hello Batnoob,

    Will the user be able to change the equation when using this function?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: function argument variables

    Please Login or Register  to view this content.
    Ben Van Johnson

  9. #9
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    Re: function argument variables

    Quote Originally Posted by Leith Ross View Post
    Hello Batnoob,

    Will the user be able to change the equation when using this function?
    Yes, the user should be able to change the equation. the equation is an argument so user should be able to input anything e.g.
    =SIGMA(9,1,2*i)
    =SIGMA(12,-7,4*i+6)
    etc.
    Last edited by Batnoob; 03-05-2018 at 11:58 PM.

  10. #10
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    Re: function argument variables

    Ben, like the rest of everything i tried, i just got a #Value error, when i tried your suggestion.
    PS, I clarified things in my previous explanatory post

  11. #11
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: function argument variables

    If Equation is passed as a string, it must be entered as such - with QUOTES. In a cell: =SIGMA(5,1,"4*i+6")
    And here's the UDF (which assumes you actually wanted to use the incremented "a" value, not the static "i" value):
    Please Login or Register  to view this content.
    BTW, your original code was needlessly recursive. Just accumulate the value in the loop and assign it to SIGMA after.
    Last edited by leelnich; 03-06-2018 at 02:55 AM.

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: function argument variables

    Quote Originally Posted by leelnich View Post
    BTW, your original code was needlessly recursive. Just accumulate the value in the loop and assign it to SIGMA after.
    There was no recursion in the original code. This line
    Please Login or Register  to view this content.
    is not recursive- Sigma is effectively a local variable not a function call.

  13. #13
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: function argument variables

    Quote Originally Posted by xlnitwit View Post
    Sigma = Sigma + S
    is not recursive- Sigma is effectively a local variable not a function call.
    I tested that- and you are correct. But I confess to not understanding why. Is it because the syntax lacks parenthesis - or parameters for that matter?

    Regardless of the answer, the extra variable can be dropped:
    Please Login or Register  to view this content.
    Last edited by leelnich; 03-06-2018 at 12:07 PM.

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: function argument variables

    Yes, without the brackets it's just a variable- as when you use it to return the value at the end.

  15. #15
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: function argument variables

    Thanks for pointing it out. I never really considered that implication when using functions - some recursive - in the past.

  16. #16
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: function argument variables

    You're welcome.

    I like your avatar- I often feel like hitting my laptop like that to make the code do what I want.

  17. #17
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: function argument variables

    Like iron smithing, the process of forging code is learned by observing the properties of your raw materials ... or something like that.

  18. #18
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    Re: function argument variables

    i am so confused. so i want the user to be able to type in the cell
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    and it calculates (in this case to 6, because 2*1+2*2=6), but instead everything i've gotten returns #VALUE
    does the user have to wrap the variable that they put in the equation with special symbols to correctly pass it to VBA (kind of like batch %var% or POSH's $var)?
    :: BatNoob
    ' BatNoob
    /* BatNoob*/

  19. #19
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: function argument variables

    As leelnich said, they will have to put the equation itself in quotes.

  20. #20
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: function argument variables

    This goes in a cell:
    =SIGMA(2,2,"2*i")

    ...and this is your User-Defined Function (macro):
    Please Login or Register  to view this content.
    Last edited by leelnich; 03-08-2018 at 02:39 AM.

  21. #21
    Registered User
    Join Date
    02-26-2018
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2016, 365
    Posts
    28

    Re: function argument variables

    thank you both! +1 reputation each.

  22. #22
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: function argument variables

    You're most welcome, thank you for the rep! If concluded, please mark your thread as SOLVED (Thread Tools above post #1). Regards – Lee

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 01-07-2015, 06:29 AM
  2. [SOLVED] ByRef argument type mismatch / Passing array as a function argument
    By pzling in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2012, 06:23 PM
  3. Replies: 3
    Last Post: 09-11-2012, 02:03 AM
  4. Function argument
    By fredlake in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-04-2010, 06:16 PM
  5. Argument for a Function
    By Rainmaker in forum Excel General
    Replies: 3
    Last Post: 07-25-2007, 08:03 PM
  6. Need Function Argument
    By pasekm in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-18-2006, 07:20 PM
  7. [SOLVED] Function (array argument, range argument, string argument) vba
    By Witek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-24-2005, 11:07 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1