Hiding and Showing a Command Bar

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

You hide or show a toolbar by using the CommandBar object's Visible property. When you display a toolbar, you can specify where it will appear on the screen by using the Position property. For example, the following code sample takes three arguments: the name of a toolbar, a Boolean value indicating whether it should be visible or hidden, and a value matching an msoBarPosition constant specifying where on the screen the toolbar should be displayed. The sample code also illustrates how to use the CommandBar object's Type property to make sure the specified command bar is a toolbar:

Function CBToolbarShow(strCBarName As String, _
                       blnVisible As Boolean, _
                       Optional lngPosition As Long = msoBarTop) As Boolean
                  
   ' This procedure displays or hides the command bar specified in the
   ' strCBarName argument according to the value of the blnVisible
   ' argument. The optional lngPosition argument specifies where the
   ' command bar will appear on the screen.
   
   Dim cbrCmdBar As CommandBar
      
   On Error GoTo CBToolbarShow_Err
   
   Set cbrCmdBar = Application.CommandBars(strCBarName)
   
   ' Show only toolbars.
   If cbrCmdBar.Type > msoBarTypeNormal Then
      CBToolbarShow = False
      Exit Function
   End If
   ' If Position argument is invalid, set to the default
   ' msoBarTop position.
   If lngPosition < msoBarLeft Or lngPosition > msoBarMenuBar Then
      lngPosition = msoBarTop
   End If
   
   With cbrCmdBar
      .Visible = blnVisible
      .Position = lngPosition
   End With
   
   CBToolbarShow = True

CBToolbarShow_End:
   Exit Function
CBToolbarShow_Err:
   CBToolbarShow = False
   Resume CBToolbarShow_End
End Function

You display a custom menu bar by setting its Visible property to True and setting the existing menu bar's Visible property to False.

See Also

Working with Command Bars | Manipulating Command Bars and Command Bar Controls with VBA Code | Getting Information About Command Bars and Controls | Creating a Command Bar | Copying a Command Bar | Deleting a Command Bar | Preventing Users from Modifying Custom Command Bars | Working with Personalized Menus | Working with Images on Command Bar Buttons | Working with Command Bar Controls | Working with Command Bar Events