+ Reply to Thread
Results 1 to 14 of 14

How to use constants stored in VBA in a regular formula?

  1. #1
    Registered User
    Join Date
    01-01-2010
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    34

    How to use constants stored in VBA in a regular formula?

    Hello everybody,

    My google skills abadon me here.... I tried to search for keywords like "use constants stored in VBA in a regular formula". A lot of hits of course but not the constants I'm looking for.

    The case: I'd like to hide a table of steel profiles in VBA and let the user of the model use them as if they were named cells in a formula. I'd prefer not to make a table in an Excel sheet and hide it (or very hidden).

    I tried:

    Please Login or Register  to view this content.
    example1.png

    But Excel doesn't recognise these. Could it maybe has something to do with the name? Maybe Excel thinks this is a coordinate?
    However HEB_200 doesn't work either:
    Please Login or Register  to view this content.
    example2.png

    Which gives #NAME?
    example3.png

    Does anyone have a suggestion to help me?

    Thank you in advance!

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: How to use constants stored in VBA in a regular formula?

    You can add names to your worksheet and you can read this article to get further details.
    Apparently you can also hide them so they don't show up in the name manager sheet.

    Please Login or Register  to view this content.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: How to use constants stored in VBA in a regular formula?

    You need to declare global variable in declaration part in the code module, not in the procedure.

    e.g
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-01-2010
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    34

    Re: How to use constants stored in VBA in a regular formula?

    Thank you for your swift answers! I was hoping for a solution the users don't need VBA for. Everywhere in the calculation one should be able to use steel sections in their calculations. Like native Excel formulas as being created by custom formulas. Maybe a bit like =7*Pi(). Pi is a constant everybody can use in Excel.

    Creating named cells isn't feasible as buildings or bridges may contain many different profiles:
    example4.png

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: How to use constants stored in VBA in a regular formula?

    Do you want it as an UDF?

    e.g.
    Please Login or Register  to view this content.
    In cell
    =D6*myPi()

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

    Re: How to use constants stored in VBA in a regular formula?

    If you don't want to use VBA, you can create a named constant using the "define name" dialog: https://www.techrepublic.com/article...med-constants/

    I have used jindon's UDF approach, too. In those few cases, they are constants that I want available to all of my worksheets, and I don't want to bother defining a named constant each time I create a new workbook. In those cases, not only do I use a VBA UDF, but I have stored the VBA UDF inside of an add-in that I can install and make those constants (and other UDFs I need) available to all of my workbooks.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: How to use constants stored in VBA in a regular formula?

    Creating named cells isn't feasible as buildings or bridges may contain many different profiles:
    Just so we're clear, the solution I gave you doesn't create a named range it creates a name that's associated with a value.
    Please Login or Register  to view this content.
    So after running this code you have names in your workbook that a user can type into any cell and when you hit enter it will display the value and it can be used in formulas.
    In the case above if a user types =HEB_200 in any cell and presses enter it will display the value.
    So =HEB_200 + 1 will display 84.2
    I also gave you a link to the page about names which shows that if you use visible = false the user won't see the names in the name list and therefore can't delete them by accident.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-01-2010
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    34

    Re: How to use constants stored in VBA in a regular formula?

    Yes, that functionality of a UDF is what I meant. However this doesn't seem to be a logical method for many hundreds of constants. I was more thinking of a table type or list structure in VBA.

    Maybe a veryhidden Excelsheet than is a better way forward? I could make a macro to give all the steel profile types a name. Then everybody can just type any L or H or I profile they want. The visible = false trick was new for me. Do you see any limitations by using so many named cells? Maybe Excel will slow down?

    Thank you all!
    Last edited by ESF; 02-02-2023 at 05:22 AM. Reason: typo

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: How to use constants stored in VBA in a regular formula?

    Still not clear though
    Please Login or Register  to view this content.
    =D6*myPi("HEB280")

  10. #10
    Registered User
    Join Date
    01-01-2010
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    34

    Re: How to use constants stored in VBA in a regular formula?

    This example is really good and as I had in mind:
    Quote Originally Posted by jindon View Post
    =D6*myPi("HEB280")
    I think I'll start small with the 'case' example. Instead of trying to be comprehensive with the full table Steel sections.xlsx

    With your help I've made different functions and it works great! Thank you all!
    Please Login or Register  to view this content.
    =D6*wbHEB(360)

  11. #11
    Registered User
    Join Date
    01-01-2010
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    34

    Re: How to use constants stored in VBA in a regular formula?

    Sorry to come back to my already solve question this morning. The tool works as a charm, so I thought to expand the functionality...

    I tried to assign multiple constants to a single variable. In this case the mass ánd surface area of a profile.
    Unfortunately now the user can't just type the size but need to use ""
    Please Login or Register  to view this content.
    My workbook now only shows the area, not the mass:
    109.3 =wbSheetpile("AZ18-700","")
    0 =wbSheetpile("","AZ18-700")

    Any suggestions are highly appreciated!

  12. #12
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to use constants stored in VBA in a regular formula?

    Maybe :
    Please Login or Register  to view this content.
    Last edited by karedog; 02-03-2023 at 12:47 PM.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  13. #13
    Registered User
    Join Date
    01-01-2010
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    34

    Re: How to use constants stored in VBA in a regular formula?

    Oops... an ara is a macaw in Dutch... sorry for this blooper.

  14. #14
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to use constants stored in VBA in a regular formula?

    Not to worry about it.
    Last edited by karedog; 02-05-2023 at 02:37 AM.

+ 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. Use constants in a formula
    By MonicaMacD in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-07-2014, 02:26 AM
  2. UDF intellisense constants in formula bar. Possible with vba?
    By spetty in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2012, 04:30 AM
  3. Counting constants between constants
    By po07pro in forum Excel General
    Replies: 2
    Last Post: 10-13-2011, 06:02 AM
  4. Converting weeknumber formula to VBA (array constants)
    By opopanax666 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-14-2008, 06:23 PM
  5. Copying formula cells that really ought to be constants?
    By Nate in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-27-2006, 08:55 AM
  6. Formula Array Constants, Problem
    By pegbol in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-01-2005, 02:45 PM
  7. How do you copy a formula without incrementing some constants?
    By JohnT in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-18-2005, 10:06 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