Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Hide/Show all tabs on Ribbon except custom tab

Tags:

excel

vba

ribbon

How can I hide and show all the standard Excel ribbon tabs using VBA (not XML). I do not want to hide the whole ribbon (as is asked here: VBA minimize ribbon in Excel) just the tabs. I know how to use startFromScratch using XML so please do not suggest that or other XML solutions.

So far I have done an extensive Google search and looked at:

  • http://msdn.microsoft.com/en-us/library/office/ee390805(v=office.11).aspx
  • http://msdn.microsoft.com/en-us/library/microsoft.office.tools.ribbon.officeribbon.startfromscratch.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
  • Customizing a ribbon with VBA in Excel
  • Show Excel 2007 Ribbon in XLS file using Excel VBA
  • Show Excel 2007 Ribbon in XLS file using Excel VBA
  • Ribbon GUI Guidelines
  • Excel CustomUI ribbon layout
  • http://www.rondebruin.nl/win/s2/win012.htm

What I am saying is I have already done an extensive search and tried many things without getting a result.

like image 222
Brett Avatar asked Nov 14 '13 00:11

Brett


3 Answers

You can indeed hide/show ribbons using VBA. Here is an example:

<ribbon startFromScratch="false">
  <tabs>
  <!-- EXCEL BUILT-IN TABS -->
    <tab idMso="TabDeveloper" getVisible="GetVisible">
	  <group idMso="GroupCode" visible="true"/>
	  <group idMso="GroupAddins" visible="true"/>
	  <group idMso="GroupControls" visible="true"/>
	  <group idMso="GroupXml" visible="true"/>
	  <group idMso="GroupModify" visible="true"/>
	</tab>
  </tabs>
</ribbon>
  1. Setup your XML file.

  2. Setup your VBA script. Sub GetVisible(control As IRibbonControl, ByRef MakeVisible) Select Case control.ID Case "TabDeveloper": MakeVisible = True Case "TabHome": MakeVisible = True Case "TabInsert": MakeVisible = True Case "TabPageLayoutExcel": MakeVisible = True Case "TabFormulas": MakeVisible = True Case "TabData": MakeVisible = True Case "TabReview": MakeVisible = True Case "TabView": MakeVisible = True Case "TabAddIns": MakeVisible = True Case "TabBackgroundRemoval": MakeVisible = True End Sub

  3. Download this file for a list of Control IDs for MS Office. http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=6627

Tip: Be sure to close your Excel Workbook that includes the ribbon you are editing before editing the XML ribbon file. I have found that it sometimes erases my VBA code (I don't know why, it just does).

like image 51
user6620568 Avatar answered Oct 14 '22 17:10

user6620568


Try this XML for Excel I have tested:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
    <tab idMso="TabHome" visible="false" />
    <tab idMso="TabInsert" visible="false" />
    <tab idMso="TabFormulas" visible="false" />
    <tab idMso="TabData" visible="false" />
    <tab idMso="TabReview" visible="false" />
    <tab idMso="TabView" visible="false" />
    <tab idMso="TabDeveloper" visible="false" />
</tabs>
</ribbon>
</customUI>

XML Code

Result:
ExcelApp

like image 42
PatricK Avatar answered Oct 14 '22 15:10

PatricK


How can I hide and show all the standard Excel ribbon tabs using VBA (not XML)

The answer is "YOU CAN'T".

AFAIK, you can't do that using VBA. Unfortunately VBA doesn't expose the tabs. The only options that you have are as shown in the image below

enter image description here

So you can work with the commandbar, commandbarButton, commandbarComboBox etc...

You can say that Set cbar = Application.CommandBars("Ribbon") but after that, the problem that you will face is how to get a handle for the tabs.

What you can do with the Ribbon using VBA:

  • Determine whether a particular control is Enabled/Visible/Pressed(Toggleboxes/CheckBoxes)
  • Get a control's label, screen tip, or supertip Display the image associated with a control.
  • Execute a particular control.

What you can't do with the Ribbon using VBA:

  • Determine which tab is currently selected.
  • Activate a particular tab.
  • Hide a particular tab
  • Add a new tab.
  • Add a new group to a tab.
  • Add a new control.
  • Remove/Disable/Hide a control.

You can however use XML to achieve what you want. For example

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon>
        <tabs>
            <tab idMso="TabReview" visible="false" />
        </tabs>
    </ribbon>
</customUI>

But I guess you do not want to go via the XML Route.

like image 40
Siddharth Rout Avatar answered Oct 14 '22 15:10

Siddharth Rout