+ Reply to Thread
Results 1 to 9 of 9

Calling and Excel Constant by A Variable

  1. #1
    Registered User
    Join Date
    11-06-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Question Calling and Excel Constant by A Variable

    Very simple question:
    Given a userform with ComboBoxHorizontalAlign populated with the values "Left", "Right", and "Center", and a current Value of "Left"

    Please Login or Register  to view this content.
    What am I missing?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling and Excel Constant by A Variable

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-06-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Calling and Excel Constant by A Variable

    Sigh..... I know I can do that..... is that really the only way? I have to trade out one clean line of code for a lengthy Case statement?

    I appreciate the help, shg.

  4. #4
    Registered User
    Join Date
    11-06-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Calling and Excel Constant by A Variable

    It gets worse in another place that I hadn't mentioned before: Paper Sizes.
    There are Excel constants for approximately 40 different paper sizes. I have a function that queries my printer and checks to see what page sizes are available. It would sure be nice to dump an item selected from that list straight to the .PaperSize item, instead of having to write a Select with 40 Cases.

    I know... I'm whining... and hoping someone has a better way.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling and Excel Constant by A Variable

    In VBA, name binding of constants and simple variables to the identifiers that reference them occurs at compile time; i.e., they are statically bound.

    You could put them in a collection, using left/right/center/whatever as the key and the enumeration value as the item:

    Please Login or Register  to view this content.
    Last edited by shg; 12-09-2014 at 08:15 PM.

  6. #6
    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: Calling and Excel Constant by A Variable

    Quote Originally Posted by diakonos1984 View Post
    ....There are Excel constants......
    I think with that statement you've probably answered your own question. A constant is not a string of text that can be built up by combining other strings, it's an integral and indivisible reference to an object's property.
    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.

  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: Calling and Excel Constant by A Variable

    Hello diakonos1984,

    Maybe this option would be more appealing...
    Please Login or Register  to view this content.
    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
    Registered User
    Join Date
    11-06-2009
    Location
    Arkansas
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Calling and Excel Constant by A Variable

    Thank you for those helpful suggestions.
    I may use Leith's concise Switch for the alignments and shg's Collection item for the paper sizes.

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Calling and Excel Constant by A Variable

    The paper size constants range from 1 to 41 (excluding xlPaperUser which is 256) so if you load the combobox in the correct order, you could simply add 1 to the ListIndex to get the correct xlPaperSize value.
    Remember what the dormouse said
    Feed your head

+ 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. Call a constant/variable by creating another variable
    By pienaaj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-12-2013, 10:36 AM
  2. Sum of constant + variable = constant
    By neurogirl900 in forum Excel General
    Replies: 3
    Last Post: 12-19-2011, 11:02 PM
  3. Making a constant and a variable in excel 2007
    By AKW in forum Excel General
    Replies: 3
    Last Post: 11-23-2008, 03:26 PM
  4. How to average one constant # to variable #s? A1:A2 A1:A3 A1:A4
    By Colfax in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-01-2006, 10:55 PM
  5. [SOLVED] Variable Acting Like a Constant?
    By George Boynton in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2005, 12:05 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