Resetting Multiple Comboboxes To ListIndex 0

jbrown2424

New Member
Joined
Mar 10, 2022
Messages
8
Office Version
  1. 365
I have a workbook with >100 comboboxes and would like to have a command button reset all of these to listindex = 0.
Do I have to list each of these comboboxes individually...

Combobox1.listindex = 0: Combobox2.listindex = 0:.....................Combobox100.listindex = 0

or is there a way to reset all of these with a simplified, shortened code?

Thank you
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Where are they located? One worksheet? Many worksheets? A UserForm?
 
Upvote 0
This will set all comboboxes in all sheets to 0. If you want to limit it to just those two the code needs a tweak.

Be advised that setting the Listindex to 0 will select the first item in the list.
VBA Code:
Public Sub ResetComboboxes()

    Dim WS As Worksheet
    Dim O As OLEObject
    
    
    For Each WS In Worksheets
    
      For Each O In WS.OLEObjects
      
         If TypeName(O.Object) = "ComboBox" Then
            O.Object.ListIndex = 0
         End If
         
      Next O
      
   Next WS

End Sub
 
Upvote 0
This will set all comboboxes in all sheets to 0. If you want to limit it to just those two the code needs a tweak.

Be advised that setting the Listindex to 0 will select the first item in the list.
VBA Code:
Public Sub ResetComboboxes()

    Dim WS As Worksheet
    Dim O As OLEObject
   
   
    For Each WS In Worksheets
   
      For Each O In WS.OLEObjects
     
         If TypeName(O.Object) = "ComboBox" Then
          [COLOR=rgb(247, 218, 100)]  [/COLOR][COLOR=rgb(41, 105, 176)]O.Object.ListIndex = 0[/COLOR]
         End If
        
      Next O
     
   Next WS

End Sub

The code is currently setting the ListIndex of roughly one-third of the comboboxes to 0 and then I get an error:

I get the following:
run-time error '380'
Could not set the ListIndex property. Invalid property or value.

Upon clicking debug, it highlights: O.Object.ListIndex = 0

Any thoughts?

Also, how would I tweak this code to only reset the comboboxes on sheets6 and sheet14?

Thank you for your help!
 
Upvote 0
I can't troubleshoot that error without your file.

For two specific sheets you can do this. Are Sheet6 and Sheet14 the tab names or the code names? I am going to assume they are tab names.
VBA Code:
Public Sub ResetComboboxes()

   ResetSheetComboboxes WS:=Worksheets("Sheet6")
   ResetSheetComboboxes WS:=Worksheets("Sheet14")
   
End Sub

Public Sub ResetSheetComboboxes(WS As Worksheet)

    Dim O As OLEObject
    
   For Each O In WS.OLEObjects
   
      If TypeName(O.Object) = "ComboBox" Then
         O.Object.ListIndex = 0
      End If
      
   Next O

End Sub
 
Upvote 0
I can't troubleshoot that error without your file.

For two specific sheets you can do this. Are Sheet6 and Sheet14 the tab names or the code names? I am going to assume they are tab names.
VBA Code:
Public Sub ResetComboboxes()

   ResetSheetComboboxes WS:=Worksheets("Sheet6")
   ResetSheetComboboxes WS:=Worksheets("Sheet14")
 
End Sub

Public Sub ResetSheetComboboxes(WS As Worksheet)

    Dim O As OLEObject
 
   For Each O In WS.OLEObjects
 
      If TypeName(O.Object) = "ComboBox" Then
         O.Object.ListIndex = 0
      End If
   
   Next O

End Sub

I can't troubleshoot that error without your file.

For two specific sheets you can do this. Are Sheet6 and Sheet14 the tab names or the code names? I am going to assume they are tab names.
VBA Code:
Public Sub ResetComboboxes()

   ResetSheetComboboxes WS:=Worksheets("Sheet6")
   ResetSheetComboboxes WS:=Worksheets("Sheet14")
 
End Sub

Public Sub ResetSheetComboboxes(WS As Worksheet)

    Dim O As OLEObject
  
   For Each O In WS.OLEObjects
 
      If TypeName(O.Object) = "ComboBox" Then
         O.Object.ListIndex = 0
      End If
    
   Next O

End Sub
I don't expect you to go through every line of the workbook to figure this out.
Any idea what the error code "run-time error '380', Could not set the ListIndex property. Invalid property or value." typically points to so I know what type of error I am looking for?

Sheet14 and Sheet6 are the code names. The tab names are Account Information and Product Overview
 
Last edited:
Upvote 0
Typically it would mean that the object is not a combobox (or listbox). But there is a check for that so I don't know how it's possible. When the code stops I would go into debug mode and query O.Object.Name to see what the object is and what sheet it's on.
 
Upvote 0

Forum statistics

Threads
1,214,873
Messages
6,122,029
Members
449,061
Latest member
TheRealJoaquin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top