+ Reply to Thread
Results 1 to 9 of 9

Function, sub or something else for specific problem

  1. #1
    Registered User
    Join Date
    12-21-2017
    Location
    Poland
    MS-Off Ver
    2010
    Posts
    4

    Function, sub or something else for specific problem

    Dear all,

    Under link: https://imgur.com/a/Hc2nP you can see image from simple excel file (overview of what I would like to get).

    I would like to make excel file in which I will have a lot of inputs (and later on I would like to have possibility to add more inputs). In VBA I would like to make code for specific calculations which will be using all inputs and computing few values. For eg. I would like to print into excel cells few of this calculated values. I made two cases:

    First with functions. I do not like that solution because if I will print into cells 100 values I need to have 100 functions and change each exactly the same function except one function.

    Second with Sub. Here on the other hand I have one code, but I need to click button to execute calculations and I do not like this because I would like to execute calculations on the fly like in case of functions (change in input variable equals automatic change in each output).

    Could you give me a tip what is best way to do this?
    Best regards,
    Pawel
    Last edited by APaul_; 12-21-2017 at 04:14 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Function, sub or something else for specific problem

    Pawel

    Why do you have (almost) identical functions, output1 and output2?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-21-2017
    Location
    Poland
    MS-Off Ver
    2010
    Posts
    4

    Re: Function, sub or something else for specific problem

    Since I am interested in one large code (one function?) to which I can read all my input data, and in next step calculate many equations and get a lot of results.

    When I find that I am interested in, for example, the calculated variable A, I want to display it in excel. I suppose that with the function I can display only one variable?

    If I find that I want to display the another calculated variables B, C, D ... in cells, I assume that I have to copy the same function (in which I have all results) and as a return instead of A choose B and in each next (the same) function another variable.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,825

    Re: Function, sub or something else for specific problem

    I don't see very much to go on to give specific suggestions. A few questions:
    1. What does this mean?
    I do not like that solution because if I will print into cells 100 values I need to have 100 functions and change each exactly the same function except one function.
    I am not sure what your concern here is.
    I could see combining your 2 functions into one single function with an additional input value.
    Please Login or Register  to view this content.
    If you are unfamiliar with the select case statement: https://msdn.microsoft.com/en-us/VBA...case-statement
    2)
    I suppose that with the function I can display only one variable?
    UDF's that return only one value are easier to code, but UDF's are not limited to returning single values. A UDF can return an array of values, similar to the built in functions LINEST() and FREQUENCY(). I have an example here discussing how to code an array function UDF: https://www.excelforum.com/excel-pro...-function.html

    I guess at this point I see two somewhat related questions -- how to add a toggle to a UDF so that you can specify which output of several possible outputs the UDF returns, or how to code a UDF so that it can output multiple results. Am I understanding correctly? Is there something I am misunderstanding?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    12-21-2017
    Location
    Poland
    MS-Off Ver
    2010
    Posts
    4

    Re: Function, sub or something else for specific problem

    I apologize for inconsistent question. Exactly, combining two functions with toggle is solution for my problem in case of using functions. I did not think about that.

    My code looks like this (and it will grow over time).
    Please Login or Register  to view this content.
    So far, to get for eg. the value kg_C instead of m_air, I copied the whole function and changed the return. Indeed, toggle will help me a lot.

    After all, I'm afraid that in the case of functions , after some time I will not be able to control the input data (if I add another input data, I will have to add it to every field with this function in Excel). For this reason, I also considered using Sub. It seems to me that it would be easier to manage input data, but I do not know how automatically run Sub (I can run it through the button). Maybe there is an option of an automatic Sub without a button?

    Thank you very much for the answers.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,825

    Re: Function, sub or something else for specific problem

    Maybe there is an option of an automatic Sub without a button?
    There are event procedures (change and calculate event seem applicable): http://what-when-how.com/excel-vba/a...-in-excel-vba/ and/or http://www.cpearson.com/Excel/Events.aspx

    An interesting problem. I don't know that I have a good, final answer to it. I know that I sometimes struggle with this same kind of question. Is it better to program one large procedure with multiple toggles and decision trees and branches to handle every possible scenario that I expect to encounter, or to have several smaller procedures where each one is programmed to handle a specific part or branch of the task. I don't know that there is a single "best" answer to this question. In my own programming, I find myself kind of uncomfortable with the idea of a single large procedure that expands with each new wrinkle I throw at the problem. I would be interested in other experienced programmers' thoughts on this, but I can see some value in carefully planning how you want to code the procedure for the current project and make it expandable for future projects.

    Part of this might be about what you mean by "expand". For example, you have a "table" of compounds and molecular weights hard coded into the procedure. Sometimes, "expand" might mean simply that I want to be able to use more or fewer compounds in the list. In those cases, I might prefer to have the table of compounds in the spreadsheet (perhaps identified with a suitable named range). Then, when I want to expand the list of compounds, I don't need to edit the VBA code, I can simply edit the table of compounds and the code can respond accordingly.

    At this point, I might suggest that you step back and look at the project as a whole. What task(s) does it need to perform now? How will it need to expand?

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Function, sub or something else for specific problem

    Pawel

    Crazy idea here, but why not explain what you are actually trying to do?

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,780

    Re: Function, sub or something else for specific problem

    And whilst you are at it, please take a moment to re-read forum rule #1 and then amend your thread title to something that better explains your problem. Changing your thread title is not optional, which means you must change it. Thanks!

    • Use concise, accurate thread titles.
    • Your post title should describe your problem, not your anticipated solution.
    • Use terms appropriate to a Google search - poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice tell us nothing.
    • Responding to a request to change your thread title by doing so is mandatory.

    To change a title go to your first post, click EDIT then Go Advanced and change your title.

    No help to be offered, please, until the OP complies with this request.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  9. #9
    Registered User
    Join Date
    12-21-2017
    Location
    Poland
    MS-Off Ver
    2010
    Posts
    4

    Re: Function, sub or something else for specific problem

    MrShorty thank you very much for your answers i find it very helpful.

    Sorry for wrong title (but i have no idea how to describe my problem) and unclear questions. I think i received answers for my problem, please close the topic.

    Best regards,
    Paweł

+ 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. Specific Search Problem
    By kevin.matheny in forum Excel General
    Replies: 4
    Last Post: 12-04-2015, 03:47 PM
  2. [VBA] Problem with outmail function - function sends mails only to 1 recepient
    By sauron12 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-21-2015, 03:30 PM
  3. [SOLVED] Function possible for Using specific number to count other specific numbers total ?
    By Karnik in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-02-2015, 03:35 AM
  4. Replies: 6
    Last Post: 10-20-2013, 07:16 PM
  5. Replies: 0
    Last Post: 09-28-2013, 11:21 AM
  6. Function to write Data to DB from specific rows and colomuns Plus specific Timestamp
    By Mudhafar.M in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-18-2013, 08:01 PM
  7. Problem with Indexing a specific Row
    By Jogier505 in forum Excel General
    Replies: 3
    Last Post: 04-11-2011, 02:27 PM

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