Set the value selected for an ActiveX ComboBox using VBA

I am trying to set the value selected for an ActiveX Control Box using VBA to the first item in the list or blank. When recording the macro no code is produced for the action.
tjd71Asked:
Who is Participating?
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

Set the ListIndex property of the ComboBox to 0 (zero).

For example:
Option Explicit
Public Sub Create_ComboBox1()

  ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
                             Link:=False, _
                             DisplayAsIcon:=False, _
                             Left:=50, _
                             Top:=50, _
                             Width:=100, _
                             Height:=18).Select

  ActiveSheet.OLEObjects("ComboBox1").Object.AddItem "First"
  ActiveSheet.OLEObjects("ComboBox1").Object.AddItem "Second"
  ActiveSheet.OLEObjects("ComboBox1").Object.AddItem "Third"
  ActiveSheet.OLEObjects("ComboBox1").Object.AddItem "Fourth"
  ActiveSheet.OLEObjects("ComboBox1").Object.AddItem "Fifth"
  
  ActiveSheet.OLEObjects("ComboBox1").Object.ListIndex = 0&     ' Select the first item
  
End Sub

Open in new window


BFN,

fp.
0
 
tjd71Author Commented:
Thanks FP, I am away from the office until Thursday and will try it then.
0
 
[ fanpages ]IT Services ConsultantCommented:
OK.  Thanks for letting me know.
0
 
tjd71Author Commented:
Hi Fp,

Thanks :) Too easy once you know how.

Cheers
tjd71
0
 
[ fanpages ]IT Services ConsultantCommented:
:) You're welcome.

Sometimes it is the simplest of things that become the most difficult to discover!

Good luck with the rest of your project.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.