+ Reply to Thread
Results 1 to 7 of 7

Refering to name with variable inside it

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2013
    Location
    Houston, US
    MS-Off Ver
    Excel 2007
    Posts
    22

    Refering to name with variable inside it

    Hello,

    So I have a group of comboboxes with names that go Cor_Var1_X1, Cor_Var2_X1, Cor_Var3_X1..... I want to use the number after Var in a loop such that when I set the object I can refer to it with a index like this.

    for j=1 to 10
    Set cbo_X1 = Me.OLEObjects("Cor_Var"&j&"_X1")
    'Stuff
    next j
    This gives me the Expected: list seperator or ) compile error. Is there any way around this or can I only use a index such as j at the end of a object name like this which works?

    for j=1 to 10
    Set cbo_in = Me.OLEObjects("In_Select_Var" & j")
    'Stuff
    next j
    Thank you very much for any help in advance.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Refering to name with variable inside it

    What is j declared as? what about cbo_X1?
    Last edited by Solus Rankin; 08-18-2013 at 08:30 PM.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    08-16-2013
    Location
    Houston, US
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Refering to name with variable inside it

    j is declared as a long and cbo_X1 is a OLEObject

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Refering to name with variable inside it

    Dim j As Long
    Dim cbo_X1 As OLEObject
    
    For j = 1 To 3
    Set cbo_X1 = Me.OLEObjects("Cor_Var" & j & "_X1")
    'Stuff
    Next j
    Worked fine for me. didn't feel like creating 10 checkboxes so I just reduced it to three. You do have 10 checkboxes correct?

  5. #5
    Registered User
    Join Date
    08-16-2013
    Location
    Houston, US
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Refering to name with variable inside it

    I have no idea what was wrong but when I copy and pasted your code it worked just fine after I stared at it for 5 minutes. No idea what was happeneing there. Thank you so much for your help.

    -Rob

  6. #6
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Refering to name with variable inside it

    Only difference I see is my excel automatically spaced in between the strings and the ampersands.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Refering to name with variable inside it

    I think VBA sees j& as some sort of 'in-line' (long) type declaration, or something like that.
    If posting code please use code tags, see here.

+ 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] Refering to a Sub Routine with a variable name
    By tja1964 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-01-2012, 04:15 PM
  2. Error on property ActiveSheet refering to an Excel object inside a jsp page
    By zilvia in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-13-2011, 12:47 PM
  3. Refering to sheets with Variable Names
    By mangisqa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2011, 12:42 PM
  4. Refering to sheets with Variable Names
    By mangisqa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2011, 09:08 AM
  5. Refering to sheets with Variable Names
    By mangisqa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2011, 09:07 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