Name

commandbarcombobox.Text [= setting]

Synopsis

Sets or returns the text in the edit portion of the control. The following code creates a combo box on the worksheet menu bar; if the user enters a value in the edit box, ShowText displays that value:

Sub AddTextBox( )
    Dim cb As CommandBar, cbo As CommandBarComboBox
    ' Get a command bar
    Set cb = CommandBars("Worksheet Menu Bar")
    ' Create the combo box.
    Set cbo = cb.Controls.Add(msoControlEdit, , , , True)
    ' Add a Tag so this control can be found from other code.
    cbo.Tag = "cboEditText"
    ' Display a label with the text box.
    cbo.Caption = "Enter some text"
    cbo.Style = msoComboLabel
    ' Set the procedure to run.
    cbo.OnAction = "ShowText"
End Sub
 
Sub ShowText( )
    Dim cbo As CommandBarComboBox
    ' Get the control
    Set cbo = CommandBars("Worksheet Menu Bar").FindControl _
      (msoControlEdit, , "cboEditText", , True)
    ' Display selection.
    MsgBox "You entered: " & cbo.Text
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.