VBA minimize ribbon in Excel

2020-02-09 05:05发布

I want to minimize the ribbon in Excel 2013 with VBA. I do not want to toggle the ribbon, and I do not want to hide everything including "File", "Insert", etc. I have tried several different methods, but none satisfy what I want.

This hides everything:

Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"",False)

This toggles:

CommandBars.ExecuteMso "MinimizeRibbon"

This also toggles:

SendKeys "^{F1}"

How can I simply force my ribbon to be minimized?

3条回答
迷人小祖宗
2楼-- · 2020-02-09 05:11

Not sure when you are trying to call this but this will work to minimize the ribbon if its open

If Application.CommandBars("Ribbon").Height >= 150 Then
    SendKeys "^{F1}"
End If

Open Ribbon minimum size seems to be 150 so this will only toggle if it's open

查看更多
看我几分像从前
3楼-- · 2020-02-09 05:15

If you want this to be hidden completely as soon as the workbook is opened then add this to the workbook code:

Private Sub Workbook_Open()
    Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
End Sub
查看更多
Fickle 薄情
4楼-- · 2020-02-09 05:37

Measure Ribbon height, toggle it, measure again and if taller, re-toggle. Best also to set Application.Screenupdating = false.

ht1 = Application.CommandBars("Ribbon").Height
SendKeys "^{F1}", False
DoEvents
ht2 = Application.CommandBars("Ribbon").Height
If ht2 > ht1 Then SendKeys "^{F1}", False

And I do hate it when folk question why you want to do what you want. I have a Dictator App and need 100% control over the interaction with Excel.

查看更多
登录 后发表回答