• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6352
  • Last Modified:

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.
0
tjd71
Asked:
tjd71
  • 3
  • 2
1 Solution
 
[ 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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now