+ Reply to Thread
Results 1 to 29 of 29

Writing Functions for Engineering Calcs

  1. #1
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Question Writing Functions for Engineering Calcs

    Hi all,

    Below is an example of a function in the module "Connections" that tests the allowable shear/tensile/etc. against the actual results. If I had to write a similar function repeatedly, what would be the best way for me to write the functions & the best way for me to pull the variable from user input data on a sheet.

    All advice is welcome and appreciated. Thanks


    Please Login or Register  to view this content.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing Functions for Engineering Calcs

    Not sure I understand completely but the variables for the function come from the 13 element cells on the worksheet that you include in the Excel function

    =Thru_Bolt(element1, element2....etc)
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    Hi yes,

    The idea (as of right now) is:

    1. User fills out relevant cells
    1. a - presses button or keyboard combination to run the subroutine that contains the location of the variable and feeds it into the function (the reason for the sub is because the variables will be used for more than just calculating one function)

    2. Results output next to input cells

    3. Outputs results
    3. a. Results are stored for other functions on the sheet, i.e., creating graphics, charts, etc.

  4. #4
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    I don't completely understand you desired result, but from the function, it looks like you are trying to figure out if the criteria you enter into the cells results in an ok bolt, or an overstressed bolt. I don't know all the definitions of the criteria you are using so I don't know if this works right or not. You'll just have to try it and see. There is no button or macro to run. It's just a formula in cell D9 that results in either OK, or Overstressed.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    Yeah, I appreciate your help and setting it up like that...
    This calc checks the capacity of a "thru-bolt" similar equations would be for "weld, wood connection, adhesive, ballast mount, brick, steel plate, platform."

    Each has a different set of input variables and outputs - in my "Structural Template," -

    I have tabs that go to the section that the user clicks on - there, they fill out the information, and in the background, the results are formatted to go on a final report.

    I do like the way you have it set up (named cells, I'd add a button to leave the formula off the page), do you think that's the best way to go about it at scale?

    My questions are:
    What's the best way to organize all of these functions in the VBA editor?
    Should I have all the input variables defined as cells on a sheet?
    Should I write individual functions?
    Should I have one sub-routine that defines the variables as range. value?


    I really don't want to make the same mistake of writing 50+ equations of a garbled mess as a result in the end.

  6. #6
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    The full process is as follows (code already written). I don't expect you to go line-by-line, but this will give you a good idea of the process. Thankfully the next 20+ functions are all mostly straightforward.

    I wrote the code below, but I'm sure someone has done something similar in a far more organized way. I don't want to organize what I've already written, but I want something more concise moving forward - no codes required, just advice is perfect.

    1. Functions that calculate structural stuff

    Please Login or Register  to view this content.


    2.Call functions & create a table:


    Please Login or Register  to view this content.
    3. Create a string of the equation and result:


    Please Login or Register  to view this content.

    4. Save the resulting diagrams as PNGs:


    Please Login or Register  to view this content.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing Functions for Engineering Calcs

    The question is why are you using a VBA function to get this result at all?

    You're just creating a User Defined Function where non is necessary and slowing things down. Just use standard Excel functions, i.e.

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    A few reasons:

    1. I don't want formulas on the sheet at all.

    2. Many employees will use this workbook & I want to mitigate them accidentally changing the formulas. I also don't want to lock thousands of cells to prevent this.

    3. Congesting cells with formulas makes tracking the whole process more challenging (for me). I'm not just looking for the "OK" vs. "Overstressed" results - in this same thru-bolt scenario, the next step is to calculate "Tensile Stress Adjusted for Shear" - a piecewise function with five if iterations & equations with over seven terms per equation. I could write something like that in a cell, but I'd rather not.

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing Functions for Engineering Calcs

    Quote Originally Posted by carlmon View Post
    A few reasons:

    1. I don't want formulas on the sheet at all.

    2. Many employees will use this workbook & I want to mitigate them accidentally changing the formulas. I also don't want to lock thousands of cells to prevent this.

    3. Congesting cells with formulas makes tracking the whole process more challenging (for me). I'm not just looking for the "OK" vs. "Overstressed" results - in this same thru-bolt scenario, the next step is to calculate "Tensile Stress Adjusted for Shear" - a piecewise function with five if iterations & equations with over seven terms per equation. I could write something like that in a cell, but I'd rather not.
    I don't understand. The UDF (User Defined Function) is essentially no different and on the worksheet in the same way as the standard compound formula I gave you.

    How are you currently protecting changing the UDF? I can't see any difference. If you want to protect the formula then just unlock any cells to which the use should have access and then password protect the worksheet. The formula cell can't then be changed.

    I really can't see what you are gaining. The only reason for using a UDF is if it performs something standard Excel functions can't do. They can also slow things down considerably if you have a lot.

  10. #10
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    This is an example of a longer function (below) do you think adding that to a single cell is easier? If you think so, you are probably right - I only started VBA coding ~ 6 months ago. I just cant wrap my head around putting this long of a formula in a cell.

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    If you could work that out to make a formula work with all that, it would still be better and much faster. Using built in formulas are always better than custom functions. I'm not going to try to figure all that out for a formula in a cell, but even if you use a function like this, which you absolutely can, then you should at least use "Select Case" instead of "If then" for something this long.. ie

    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    I think I understand what you're saying -

    I've attached the entire formula for one of the sections (thru bolt) & made an input/output section on the second screen shot.

    If I understand what you guys are saying correctly - it's best for me to write the calculations in the gray rectangles than go through the trouble of creating a UDF in VBA.

    The only issue that I am going to have with this method is that I need to show the intermittent calculation steps on the results page & I'm not defining them in this section.

    From your post #4... I like defining the cells based on computing the VBA function. That way the variables are each defined and not lost in longer calculations. Is that a reasonable way of going about this?
    Attached Images Attached Images
    Last edited by carlmon; 04-06-2022 at 09:09 AM. Reason: Editing post because I want to be more concise:

  13. #13
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    & Thank you for your patience. I know I can be stubborn.

    When you define the cells - the name of the cells doesn't need to match up with the function, but the order they are input into the function matters?

    & are you saying this method is poor? Because I like having each variable defined as they are used in other aspects of the worksheet. Many longer functions calculate instantaneously, so speed was never the problem.

  14. #14
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    Naming the cells will make it much easier to make your functions work and is a good idea. It is a great way to make things simpler. You should probably just go through and name every cell you will need in any function (sounds like you might need to make a list of every cell you name because it will have to be spelled exactly right any place you use it). Then you can use those cell names in formulas on the sheet, or in a function you write yourself (UDF). UDF's used to be a thing to avoid if possible mainly because they are slower than Excel's built in functions. With todays computers and processors, that's not as much of an issue anymore. You are fine doing it any way you want to whether UDF's or built in functions, but the built in functions are better and faster still. There are hundreds of built in functions. If you need to have values entered in some kind of report during the process of entering your data or whatever, and you can't use a formula in that report to reference a cells value or something for whatever reason, then yes, by all means, use a macro or UDF if you need to. I think you are trying too hard to do this the best way and can't decide which way to do it. It all boils down to using the built in functions and formulas in cells as being the best way, or UDF's with formulas in the cells as the second best, or UDF's and macros as 3rd best, but they are all fine. They all work just fine. If you want to use all UFD's, go right ahead. The only issue would be if you have too many long complicated UDF's calling each other and doing calculations which could slow down the results slightly, but that's rarely an issue. Your image you posted looks great! And yes, just put the formulas at that bottom like you pointed out using UDF's or built in functions or combination of both to display the results of the inputs above. You can lock those cells and protect the sheet so the formula cells can not be changed.

  15. #15
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    Thank you very much for your time; you're already deserving of reputation.

    I will use the combination of the UDF & built-in functions like you said. I do have one last question, can one UDF have multiple outputs? In the screenshot(attached), the Shear Stress per Bolt and Ratio share the same variables, but I'm not sure how to output two results to different cells.


    I want the ratio to be = Shear stress per bolt (value) / allowable shear strength per bolt (value)

    or do I need to write a new function & call on the previous function to get the previous values?'


    And thank you for the kind comment:D
    Attached Images Attached Images

  16. #16
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    No, 1 UDF can't have 2 final outputs. But if both cells use the same variables, you can put a different formula in each cell. For example one cell might be this:
    =Shear stress per bolt(Whatever range has this value) / allowable shear strength per bolt(Whatever range has this value)
    So if Range A1 has the value of Shear stress per bolt, and A2 has allowable shear strength per bolt then the formula would be this wherever you want the result:
    =A1/A2
    Shear stress per bolt or allowable shear strength per bolt, either one or both, can be entered by the user, or a result from another UDF or function that uses other variables entered somewhere else.


    And another cell could be this:
    =Shear stress per bolt(Whatever range has this value) * allowable shear strength per bolt(Whatever range has this value)
    I just made that up obviously but the same applies as above.
    If you need to use different values other than those 2, just use whichever ones and however many you need and put a formula in any cell to do the calculations.
    If you need to get a result from criteria that Excel has no built in function for, then you will have to write a UDF to get the result for you, then use that UDF in a cell instead of a built in one.
    UDF's can be used different ways. Either to use as a formula inserted into a cell just like you would a built in function, or to be called by a macro or another UDF to return a result so that macro can use the result to finish IT's job. I not sure if you knew this or not.

    Hope that makes sense lol
    Last edited by achammar; 04-06-2022 at 10:59 AM.

  17. #17
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    And another thing you can do is write UDFs that don't use all the variables. You would put them in as optional.. ie..

    Function Test(A as integer, Optional B as integer, C as Integer)
    Test = A + B + C

    End Function
    Then in a cell put this formula to add only the first and last variable:
    =Test(A1,,A2) - where A1 and A2 have the values to compute.
    OR this to compute 3 variables
    =Test(A1,A2,A3) - where A1 and A2 and A3 have the values to compute.
    Both of them would use the same UDF.
    I think that's how it works. I very rarely do that and I'm relying on memory :-)

  18. #18
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing Functions for Engineering Calcs

    Quote Originally Posted by carlmon View Post
    This is an example of a longer function (below) do you think adding that to a single cell is easier? If you think so, you are probably right - I only started VBA coding ~ 6 months ago. I just cant wrap my head around putting this long of a formula in a cell.

    Please Login or Register  to view this content.
    I agree, when you find yourself using more than three or four nested IF statements then it's usually possible to use a VLOOKUP table.
    For instance the lookup table would have the 1.2, 1.3....etc values in the second column and the arithmetic multipliers like
    .025 i.e. (0.1) / (4)

    .0125 i.e. ((1.75 - 1.5) / (40 - 20))

    etc. in the third column

  19. #19
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    I get what you're saying, but I don't want to have all of the outputs on the sheet. I'm trying to maintain the "clean" look of the sheet.

    How about this method:

    Write a sub routine that defines the variables i.e.
    Please Login or Register  to view this content.
    and whenever I write a function, i would call that subroutine i.e.
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    Actually the VLookup table is a good idea. You can put it in a different sheet and hide it. No one would ever know it's there.
    On the other hand, your sub and function idea above would be awesome, but it won't work as is. All the variables you want to use in your function have to be included between the parenthesis.

  21. #21
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    I believe if I declare the variables as public it should work across all modules - functions can use variables as long as they are defined somewhere.

  22. #22
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    I guess your Define variable and function combo will work in another UFD or macro, but it won't work as a formula in a cell. You could use it to write other functions or run macros though. You would have to declare every variable at the top of the module as Private or Public for it to work though, but it would work.

  23. #23
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    Well, I was wrong again... it will work as a formula in a cell also. You just can't use any variables in the formula. I guess I've never used a function without defining the variables within it. So your idea will work.

  24. #24
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    That's good news I didn't even consider putting the function in the cell itself. Look at us learning from each other!

  25. #25
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs


  26. #26
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    A cell with "=function()" only updates when that cell is refreshed (not the values)... I could write a macro that refreshes the cells, but I'm sure there is a simpler approach.

  27. #27
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    273

    Re: Writing Functions for Engineering Calcs

    Instead of writing a function to define your variables, use constants instead. That is the correct way to do it. For example, this would go at the top of your module:

    Where a-d are the variables and the number are their values
    Const a As Integer = 1
    Const b As Integer = 2
    Const c As Integer = 3
    Const d As Integer = 4

    I don't know if that will fix your problem but it might. That's the way it should be done anyway unless you have different subs that define the same variables with different values.
    You could also put this in the worksheets SelectionChange event:

    ActiveSheet.Calculate

  28. #28
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    The formula doesn't contain constants (other than pi)... I'll try the selectionchange event function


    Please Login or Register  to view this content.

  29. #29
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Writing Functions for Engineering Calcs

    I figured it out - I needed to add " Application. Volatile " at the top of each function.

+ 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. [SOLVED] Writing functions in VBA - Tutoring wanted
    By yder23 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2019, 12:21 PM
  2. Replies: 1
    Last Post: 04-16-2019, 10:43 PM
  3. Replies: 1
    Last Post: 12-04-2013, 01:04 PM
  4. [SOLVED] Excel Conditional Functions (Engineering Model)
    By rufusbabe in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-09-2013, 07:58 PM
  5. Writing Descriptions for functions
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2008, 10:42 AM
  6. engineering functions
    By isa982 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-22-2006, 02:00 PM
  7. [SOLVED] How can I use engineering functions in Excel 2003
    By kolgen in forum Excel General
    Replies: 1
    Last Post: 04-14-2005, 09:06 AM

Tags for this Thread

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