Macro enabled button VBA - 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.

Related

How to select an ActiveX Option/Radio Buttons in Form Controls

I have a Form Control with ActiveX Radio/Option Buttons in it.
The Form Control name is Side and contains Option/Radio Buttons with names xOption, oOption, and randomSide.
How would I be able to create a Macro that would allow me to set the radio buttons to a certain value upon opening the workBook. Recording a Macro of me clicking options results in a blank Macro. I've Already tried:
ActiveSheet.Shapes.Range(Array("Side")).Select
ActiveSheet.Shapes.Range("xOption").OLEFormat.Object.Value = 1
But this gives me error 1004 and other codes give me error 91. I'm really new to VBA so if I look stupid you know why.
Try something like this, using Worksheets instead of ActiveSheet:
Private Sub Workbook_Open()
Worksheets("your sheet name here").OLEObjects("xOption").Object.Value = 1
End Sub
As you want it to be selected after opening the sheet. Place this on ThisWorkbook.
You may try something like this...
ActiveSheet.OLEObjects("xOption").Object.Value = 1

How to detect if ctrl + enter in an Excel spreadsheet with VBA

My understanding is: Macro canNOT be activated while a cell is active for editing.
I notice by hitting ctrl + enter, the cell is de-activate for editing but remain selected. I would like then to run some macro refer to this cell.
Therefore, using VBA, how to detect if ctrl+ enter is pressed?
Easiest way should be using the Change event of the worksheet.
This would not (only) detect a ctrl + enter but any change of a cell.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
This sub has to be added to the worksheets code (not into a module)!
Another approach would be using the OnKey Method
Application.OnKey "^{RETURN}", "InsertProc"
See here for a decent explanation: Application.OnKey Method (Excel)

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

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.

Disable macro buttons when sheet is protected?

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