Populating Multiple Combo Boxes with the same values

BobSullivan
3StarLounger
Posts: 214
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Populating Multiple Combo Boxes with the same values

Post by BobSullivan »

In PowerPoint VBA, I'm trying to avoid entering the same code for multiple combo boxes. I'd like to do some sort of loop to populate them. I have 10 combo boxes on a slide, and all of them need the same 3 selections, "To Do", "In Progress", "Done" I understand that the code to populate one looks like this:

Code: Select all

    Slide1.ComboBox1.AddItem ("To Do")
    Slide1.ComboBox1.AddItem ("In Progress")
    Slide1.ComboBox1.AddItem ("Done")
Is there a way to do this for combo boxes 1 thru 10 without duplicating the same lines for all 10?

Thanks,
Cordially,

Bob Sullivan
Elverson, PA

User avatar
HansV
Administrator
Posts: 69475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Populating Multiple Combo Boxes with the same values

Post by HansV »

You can use code like this:

Code: Select all

    Dim i As Long
    For i = 1 To 10
        With Slide1.Shapes("ComboBox" & i).OLEFormat.Object
            .AddItem "To Do"
            .AddItem "In Progress"
            .AddItem "Done"
        End With
    Next i
Regards,
Hans

snb
3StarLounger
Posts: 241
Joined: 14 Nov 2012, 16:06

Re: Populating Multiple Combo Boxes with the same values

Post by snb »

Avoid .additem to populate a combo/listbox.

Code: Select all

Sub M_snb()
   Slide1.combobox1.List=split("To do|In Progress|Done","|")
   for j=2 to 10
      Slide1.oleobjects("combobox" & j).list=slide1.combobox1.list
   next
End Sub[\code]
Last edited by snb on 05 Jun 2020, 08:10, edited 1 time in total.

BobSullivan
3StarLounger
Posts: 214
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Re: Populating Multiple Combo Boxes with the same values

Post by BobSullivan »

:clapping: Thank you for the solution!
Cordially,

Bob Sullivan
Elverson, PA