+ Reply to Thread
Results 1 to 8 of 8

Thread: making a literal from expression

  1. #1
    Registered User
    Join Date
    11-27-2011
    Location
    CO - Colorado
    MS-Off Ver
    Excel 2010
    Posts
    4

    making a literal from expression

    Hello,

    I was just wondering if anone knew how to make a literal into an expression. In my code I have the name of the ComboBox that needs to change within a loop, however when I use that with the select case, the "UCase" makes it match "combobox#" instead of what it is representing.

    The ComboBoxes are categories; then depending on what category from the combobox is selected the number entered beside it will be added to a variable (such as the first one is restaurants) which is keeping track of the totals.

    Please let me know if anyone has any suggestions. Below I posted my code

    Thanks!


    For cboxnum = 1 To 32
    cbnum = ("ComboBox" + CStr(cboxnum) + ".value")
    
    
    Select Case UCase(cbnum)
    Case "RESTAURANTS"
    restaurants = restaurants + wksInput.Range("o3")
    Case "GROCERIES"
    groceries = groceries + wksInput.Range("o3")
    Case "COFFEE"
    coffee = coffee + wksInput.Range("o3")
    Case "ENTERTAINMENT"
    Entertainment = Entertainment + wksInput.Range("o3")
    Case "OTHER FOOD"
    ofood = ofood + wksInput.Range("o3")
    Case "RENT"
    rent = rent + wksInput.Range("o3")
    Case "CAR PAYMENTS"
    carpmts = carpmts + wksInput.Range("o3")
    Case "PARKING"
    parking = parking + wksInput.Range("o3")
    Case "GAS"
    investments = investments + wksInput.Range("o3")
    Case "OTHER BILLS"
    obills = obills + wksInput.Range("o3")
    Case "CLOTHING"
    clothing = clothing + wksInput.Range("o3")
    Case "DONATIONS"
    donations = donations + wksInput.Range("o3")
    Case "GIFTS"
    gifts = gifts + wksInput.Range("o3")
    Case "FAMILY"
    family = family + wksInput.Range("o3")
    Case "OTHER"
    other = other + wksInput.Range("o3")
    End Select
    Next
    Last edited by mthayer; 11-27-2011 at 04:26 PM. Reason: code

  2. #2
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,004

    Re: making a literal from expression

    Assuming that your combo boxes are on a user form, you can specify them with the controls property of the form.

    e.g.

    MyComboBoxName = UserForm1.Controls(0).Name
    Does this help?
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  3. #3
    Registered User
    Join Date
    11-27-2011
    Location
    CO - Colorado
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: making a literal from expression

    My comboboxes are actually in my spreadsheet (as an activex control).

    I have my sheet set up so you enter your amounts then choose the category then click done and it populates the totals. Since I am giving the user so many input fields (32 to be exact) I need to find a way to make the loop work. If I copy paste and change each variable it works but that seems silly if there is a way to loop through them all.

  4. #4
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,004

    Re: making a literal from expression

    Try

    ActiveSheet.drawingobjects(1).Name
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  5. #5
    Registered User
    Join Date
    11-27-2011
    Location
    CO - Colorado
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: making a literal from expression

    that almost worked! that's making the variable be textbox1 but I need it to be combobox1

  6. #6
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: making a literal from expression

        For cboxnum = 1 To 32
            Select Case UCase(ActiveSheet.OLEObjects("ComboBox" & cboxnum).Object.Value)
                Case "RESTAURANTS"
                    restaurants = restaurants + wksInput.Range("O3").Value
                Case "GROCERIES"
                    groceries = groceries + wksInput.Range("O3").Value
    Last edited by shg; 11-27-2011 at 05:07 PM.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    11-27-2011
    Location
    CO - Colorado
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: making a literal from expression

    that worked! thank you!

  8. #8
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: making a literal from expression

    Great! Would you please mark the thread as Solved?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0