+ Reply to Thread
Results 1 to 9 of 9

Calling and Excel Constant by A Variable

Hybrid View

  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"

    With wsTarget.UsedRange
        'Set horizontal alignment
        .HorizontalAlignment = xlLeft 'This works
        .HorizontalAlignment = "xl" & ComboBoxHorizontalAlign.Value 'This doesn't work.  I want it to.
    End With
    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

        Select Case ComboBoxHorizontalAlign.Value
          Case "Left"
            .HorizontalAlignment = xlLeft
          Case "Right"
            .HorizontalAlignment = xlRight
          Case ...
            .HorizontalAlignment = ...
        End Select
    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

    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.

  4. #4
    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.

  5. #5
    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.

  6. #6
    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:

    .HorizontalAlignment = myCollection(textbox.value)
    Last edited by shg; 12-09-2014 at 08:15 PM.

  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...
        Dim cbAlign As String
        
            With wsTarget.UsedRange
                'Set horizontal alignment
                .HorizontalAlignment = xlLeft 'This works
                cbAlign = "xl" & ComboBoxHorizontalAlign.Value
                .HorizontalAlignment = Switch(cbAlign = "xlLeft", xlLeft, cbAlign = "xlRight", xlRight, cbAlign = "xlCenter", xlCenter)
            End With
    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 Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,309

    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.
    Anyone who confuses correlation and causation ends up dead.

+ 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