Consulting

Results 1 to 5 of 5

Thread: How to concatenate ComboBox name

  1. #1
    VBAX Newbie
    Joined
    Apr 2015
    Posts
    1
    Location

    How to concatenate ComboBox name

    So I 12 combo boxes with names "ComboBox1" through "ComboBox12" respectively. Each ComboBox has the same drop-down items. Instead of me coding each one individually, is there a way to loop through them? For example, I have the following code:

    For c = 1 To 12
    combo = "ComboBox" & c
    With Sheet1.combo
    .Clear
    .AddItem "cat"
    .AddItem "dog"
    .AddItem "fish"
    .AddItem "bird"
    End With
    Next

    But it always says "combo" not recognized, or something down those lines.

    Any help/suggestions are appreciated.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    For i = 1 To 12
        With Worksheets("Sheet1").Shapes("ComboBox" & i).OLEFormat.Object.Object
            .Clear
            .AddItem "cat"
            .AddItem "dog"
            .AddItem "fish"
            .AddItem "bird"
            .ListIndex = 0 'delete this line if a default value in combobox is not desired.
        End With
    Next
    Last edited by mancubus; 04-15-2015 at 09:53 PM.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    please use code tags when posting your code.

    # button in Quick Reply will do it for you.

    [ CODE ]paste your code here[ /CODE ]

    another way:
    For i = 1 To 12
        With Worksheets("Sheet1").Shapes("ComboBox" & i).OLEFormat.Object.Object
            .Clear
            .List = Array("cat", "dog", "fish", "bird")
            .ListIndex = 3 'delete this line if a default value in combobox is not desired.
        End With
    Next
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Sub M_snb()
      sheet1.oleobjects("combobox1").object.list=Array("cat", "dog", "fish", "bird")
    
      for j=2 to 12
        sheet1.oleobjects("combobox" & j).object.list=sheet1.oleobjects("combobox1").object.list
      next
    End Sub
    NB. The use of '.clear' in combination with '.List' is redundant: '.List' replaces the existing List.

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    thanks for the reminder snb.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •