Disable macro buttons when sheet is protected? - vba

I am using shapes as macro buttons in Excel.
Even when a sheet is protected, users can click on the buttons in that sheet.
Is it possible to make it so that buttons become "disabled" when the sheet is protected?

Implement this at the beginning of every macro associated with buttons:
If Activesheet.ProtectContents = True Then
Exit Sub
End if

Related

Excel Form pops automatically when sheet opens

I have a specific worksheet with 2 sheets(Sheet1 & Sheet2). For Sheet2 I have implemented a form for the table (Using the basic Excel Form from the top bar).
My problem is that I have to make the form appear automatically every time I open Sheet1 (even if the data from the form will be completed in Sheet2).
Is this possible? Or how can I do it? (I can also use VBA)
To show the DataForm associated with a Worksheet, you use the command Worksheet.ShowDataForm (MSDN Article)
To show the DataForm for Sheet1 whenever you go to Sheet2, you can use the Worksheet_Activate event in Sheet2, like so:
Option Explicit
Private Sub Worksheet_Activate()
Sheet1.ShowDataForm
End Sub
A quick way to figure things like this out is use the "Record Macro" button, carry out the action you want, and then hit "Stop Recording" and look at the macro

VBA auto hide ribbon in Excel 2013

How to Auto-hide Ribbon in Excel 2013 in VBA? I would like to achieve exactly what I get by clicking on the upper arrow icon at the right top of Excel menu marked with blue in the picture below and then clicking on the first option marked with orange:
I would be also interested in VBA switching back to the third option Show Tabs and Commands. Important thing for me is to keep in the Excel menu the upper arrow icon (marked with blue).
I have tried hints shown in this thread: VBA minimize ribbon in Excel
but I am not satisfied with results.
Attempt 1
Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"",False)
This is good but hides the blue icon.
Attempt 2
CommandBars.ExecuteMso "MinimizeRibbon"
This is close to what I want. This keeps the blue icon but does not hide the entire menu. It switches to the second option displayed in the picture Show Tabs.
Attempt 3
SendKeys "^{F1}"
The attampt does not work at all. Moreover, it is supposed to imitate the attempt 2. So even that would not satisfy me.
I can't see that anyone else has brought this up... This isn't a workaround, this is the actual idMSO for what I think you're looking for. This code makes my excel window look like everything is gone the same way the first option does for Auto-Hide Ribbon.
Before the code runs, my window looks like this, in the 'Restore' size:
Running the following code:
Sub HideTheRibbon()
CommandBars.ExecuteMso "HideRibbon"
End Sub
Will make your window look like this, in the maxamized window size (just like what would happen if you were to press the Auto-Hide Ribbon button manually):
If you want the ribbon automatically hidden when the workbook opens, put this in the workbook code:
Sub Workbook_Open()
CommandBars.ExecuteMso "HideRibbon"
End Sub
Alternatively, to achieve the same thing, you could put this code in a module:
Sub Auto_Open()
CommandBars.ExecuteMso "HideRibbon"
End Sub
If you want the window to revert back to normal, you run the exact same code again. In other words, the following code would make no visual change at all when ran because the idMSO "HideRibbon" is a toggleButton:
Sub HideTheRibbon()
CommandBars.ExecuteMso "HideRibbon"
CommandBars.ExecuteMso "HideRibbon"
End Sub
If you want a full list of all the idMSO in excel, click the following that apply to you: Excel 2013+, Excel 2010, Excel 2007
I use this for presentation purposes
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True This is what i used to hide the ribbon
Probably you should do something a little more complicated:
Use CommandBars.ExecuteMso "MinimizeRibbon" to show/hide the ribbon.
Depending on what you want, you may show/hide all other tabs in the ribbon. E.g. use something of the code here -> Excel Hide/Show all tabs on Ribbon except custom tab
Thus 2 steps:
Step 1 - show or hide with the CommandBars.ExecuteMso
Step 2 - show or hide the rest of the tabs with some macros from the link.
A little big workaround, but you will get what you want.
I call this macro on Workbook_Open to check for the ribbon and if not hidden, it will hide the ribbon (I actually have it located in another Sub that also removes the formula bar, status bar, headings, and gridlines at Workbook_Open)...
Sub HideRibbon()
If CommandBars("Ribbon").Controls(1).Height < 100 Then
Exit Sub
Else
CommandBars.ExecuteMso ("MinimizeRibbon")
End If
End Sub
Then I call this macro on Workbook_BeforeClose to check for the ribbon and if it is not shown, it will show the ribbon for the next excel spreadsheet that is opened.
Sub ShowRibbon()
If CommandBars("Ribbon").Controls(1).Height > 100 Then
Exit Sub
Else
CommandBars.ExecuteMso ("MinimizeRibbon")
End If
End Sub
This eliminates the chance of hiding the ribbon when the workbook is opened and a user then manually showing the ribbon which in turn would reverse the show on close and actually hide the ribbon. On open, the ribbon would then be shown again. This will keep it the same every time on open and close of the workbook.
Give this a try:
Sub ShowHideRibbon()
If CommandBars("Ribbon").Controls(1).Height < 100 Then
CommandBars.ExecuteMso ("MinimizeRibbon")
Else
CommandBars.ExecuteMso ("MinimizeRibbon")
End If
End Sub
Or this:
Sub ShowHideRibbon1()
If Application.ExecuteExcel4Macro("Get.ToolBar(7,""Ribbon"")") Then
Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", False)"
Else
Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", True)"
End If
End Sub
First, go to the Excel Options, and go to "Quick Action Toolbar".
From there, search for "Hide Ribbon" and add to the toolbar. After it's on the QAT, you can call it quickly with ALT+# (on my computer it's the 8th thing, so ALT+8 will auto-hide).
Then just add a sub that does SendKeys ALT then 8:
Sub Macro1()
ActiveSheet.Activate
'Cells(1, 1).Select
SendKeys "%0", True
SendKeys "8", True
End Sub
Note: I know it's silly to have ActiveSheet.Activate, I just added that to test the macro. Depending on how it's being called, you can remove/comment out that line. The % is equivalent to ALT, and technically, I have to press 0 then 8, hence the two lines.
To get this code to work in excel 2016 you will need the following code in the "ThisWorkbook" mode.
Credit goes to BigBen - not me
Private Sub Workbook_Open()
application.CommandBars.ExecuteMso "HideRibbon"
End Sub

Excel 2010 F2-Enter Macro Button

I have a chart with two columns that count horizontally across the grid cells with a specific color fill. (=CountCellsByColor(##:##,#) There are 24 Rows in each column.
I now want to create a macro button in a particular cell so the user can click the button and the columns will auto update. Currently this is accomplished by clicking into a cell, pressing F2 then Enter, which is fine but if I can make it simpler all the better.
This is what I have, I created a commandButton from the Developer tab (Insert, Button); Then in the VBA screen the following code;
Sub RefreshCells()
Dim r As Range, rr As Range
Set rr = Selection
For Each r In rr
r.Select
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
DoEvents
Next
End Sub
I get a restricted use error that pops up after I click the button, it works once but that's it then locks the rest of the cells from editing. I have a number of VBA codes running on the sheet that allow users of the sheet to simply click certain cells to turn them different colors.
I was making this far too complicated! If I may attempt to answer my own question, here is what I did to solve this,
Under Developer Tab, I hit Record Macro Then F2 Enter followed by Stop Record and saved that function. Then I created a Button under the developer tab and assigned it that macro.
Sub Macro5()
'
' Macro5 Macro
'
'
Range("D33:D34").Select
ActiveCell.FormulaR1C1 = ""
Range("D35").Select
End Sub
It's probably not right but it seems to have the same effect of clicking into a cell, hitting F2 + Enter . The columns that don't have an automatic count update perfect, no errors, no bugs!

Invoking the Activate dialog for sheets

I want to invoke the Activate dialog for sheets in a workbook such as below. This dialog can be called manually by right clicking on the arrow button at the bottom left of Excel (2013).
I tried this:
Application.Dialogs(xlDialogActivate).Show
But instead of showing the list of sheets, it shows the list of open workbooks:
How do I call the Activate dialog for sheets?
You could create your own dialog box if you want. Create a userForm and populate it with the names of the Worksheets upon activation. You can see what the user selected through the selected function, e.g.
ListBox1.Selected(i)
You can then call a sub with the name of the Sheet and activate it, e.g.
Sub ChangeSheet(SheetName)
Worksheets(SheetName).Activate
End Sub
An alternative is to show the Sheet Command bar:
Sub ShowSheets()
Application.CommandBars("Workbook tabs").ShowPopup
End Sub
Content salvaged from comments by Scott Holtzman and Davesexcel.

Macro enabled button VBA

I'm working with VBA macros and I have a macro enabled button of which I want to disable from being clicked when the workbook is first opened. Is there anyway to do this. The macro behind it just leads to another worksheet. ![enter image description here][1]
Any help would be great
Thanks
If your button is an ActiveX Command Button, put the following code in ThisWorkbook:
Private Sub Workbook_Open()
Worksheets("Sheet1").OLEObjects("CommandButton1").Enabled = False
End Sub
Change "Sheet1" to reference the sheet that contains the button and change "CommandButton1" to reference the name of the button.