VBA code activate and deactivate toolbar, ribbon but allow copy and paste?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have the following codes that hides the ribbon and that but it also stops me from copy and pasting from another sheet.

Code:
Private Sub Workbook_Activate()

    With Application
      .ScreenUpdating = False
        .ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
        .DisplayFormulaBar = False
        .DisplayStatusBar = Not Application.DisplayStatusBar
        .ScreenUpdating = True
    End With


End Sub


Private Sub Workbook_Deactivate()
    With Application
        .ScreenUpdating = False
        .ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
        .DisplayFormulaBar = True
        .DisplayStatusBar = True
        .ScreenUpdating = True
    End With


End Sub

Does anyone know if there is a way to amend these codes but allow copy and paste to work?

Thanks

Dan
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
FWIW, I ran your code and I can still copy and paste from another sheet no problem.
 
Upvote 0
How are you trying to copy and paste?

{Ctrl} C \ {Ctrl} V ?
Right-Click + select Copy \ Right-Click + select Paste ?
Another way?- please detail

Which version of Excel are you using?
 
Last edited:
Upvote 0
Hi,

Thank you for you replies.

I've made an error on my original post.

I put I can't copy and paste from another sheet, but I meant I can't copy and paste from another workbook.

I {Ctrl} C \ {Ctrl} V to copy and paste and I am using Excel 13

Thanks

Dan
 
Upvote 0
It appears that the Activate_Workbook macro is causing your problem
- issue avoided by preventing that macro from running when the workbook is reactivated

1. Create a new standard module and rename it Ribbon
(click on Module in Project Window \ {F4} to see Properties window \ amend name to Ribbon)
2. Replace existing VBA with VBA below

Everything kept simple - hopefully not too simple!
Let us know if anything is not behaving as required

In ThisWorkbook module
Code:
Option Explicit

[COLOR=#006400][I]Private Sub Workbook_Open()[/I][/COLOR]
    Application.Run "[B]Ribbon[/B].HideRibbon"
[COLOR=#006400][I]End Sub[/I][/COLOR]

[COLOR=#006400][I]Private Sub Workbook_Activate()[/I][/COLOR]
    If RibbonHide = False Then Exit Sub
     Application.Run "[B]Ribbon[/B].HideRibbon"
[COLOR=#006400][I]End Sub[/I][/COLOR]

[COLOR=#006400][I]Private Sub Workbook_Deactivate()[/I][/COLOR]
    RibbonHide = False
    Application.Run "[B]Ribbon[/B].ShowRibbon"
[COLOR=#006400][I]End Sub[/I][/COLOR]
In Standard module (named Ribbon)
Code:
Option Explicit
Public RibbonHide As Boolean
[I][COLOR=#006400]
Private Sub HideRibbon()[/COLOR][/I]
    With Application
        .ScreenUpdating = False
        .ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
        .DisplayFormulaBar = False
        .DisplayStatusBar = Not Application.DisplayStatusBar
        .ScreenUpdating = True
    End With
[I][COLOR=#006400]End Sub[/COLOR][/I]

[I][COLOR=#006400]Private Sub ShowRibbon()[/COLOR][/I]
    With Application
        .ScreenUpdating = False
        .ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
        .DisplayFormulaBar = True
        .DisplayStatusBar = True
        .ScreenUpdating = True
    End With
[I][COLOR=#006400]End Sub[/COLOR][/I]
 
Last edited:
Upvote 0
Hi Yongle,

it's very nearly spot on.

The only issue I can see is it leaves the formula bar showing but only after it's been activated not when the file is first loaded.

I'd prefer it to be hidden but if it's needed to be this way for the copy and paste to work then I'm happy to leave it as it is.

Thanks again

Dan
 
Upvote 0
try this
goes in ThisWorbook module
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If RibbonHide = False Then
        RibbonHide = True
        Application.Run "Ribbon.HideRibbon"
    End If
End Sub


Copy & paste is still disabled in the other direction - does that require fixing too?
The way to do that would be to write a procedure
- asking user to specify "where to paste" and "range to be copied"
- copy & paste without deactivating the workbook
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top