Excel VBA - call same macro from both Ribbon and AutoOpen - excel

Having just upgraded to Excel 2013, I've moved my macros from a legacy custom toolbar to a custom ribbon menu. All works well, except for one thing. I used to have a macro that ran on AutoOpen, but could also be called manually via a button on the toolbar.
I call my macro from the ribbon using Sub myMacro(control As IRibbonControl) which works. But if I Call myMacro(control As IRibbonControl) in AutoOpen I get an "expected list separator" error. Conversely if I just Call myMacro() in AutoOpen I obviously get a "argument not optional" error. Bit of a Catch 22!
I know that I could just move my code to a third sub-routine, called by two separate macros in the ribbon and in AutoOpen, but before I admit defeat and do this I wonder if there is a way around this.
I have searched the web for a solution to this, but couldn't find anything that answered my particular query.
Thanks
Rob

A simple code as this will help?
Option Explicit
Sub AutoOpen()
Dim ctl As IRibbonControl
myMacro ctl
End Sub
Sub myMacro(control As IRibbonControl)
MsgBox "Hello World"
End Sub

Related

Excel Error on Application.OnKey Statement

I put this simple code into Visual Basic:
Sub HelloWorld()
MsgBox "Hello World"
End Sub
Sub OnKeyDemo()
Application.OnKey "^+a","HelloWorld"
End Sub
When I press CTRL+SHIFT+A on the Workbook, I get the following error:
I enabled all macros on Excel and I did a Step Into procedure recommended by others on StackExchange. Neither have worked.
Thanks!
You are getting this error because the Application.OnKey is not able to find the HelloWorld subroutine.
Microsoft's documentation does not have clear information regarding where the procedure should reside, in the general module or worksheet/workbook module.
I have tried and tested by adding this procedure in the workbook/worksheet and general module. It only works in the General Module
To add a general module:
Open the Project Explorer (Ctrl+R) and right-click on any item.
Then Select Insert->Module
In the general module add the Sub HelloWorld() procedure and it will definitely work.
Very Important
Reverse the OnKey Statement as not doing this may cause unexpected results.
For Doing this follow the below steps:
Go to the ThisWorkbook Code Pane
Above the code pane you will find two dropdowns, select Workbook in first and BeforeClose in the second.
In the added code for BeforeClose, write Application.OnKey "^+a", note the lack of ,"HelloWorld".
Omitting the ,"HelloWorld" is fine since the second parameter for the Application.OnKey procedure is optional.
What Application.OnKey "^+a" does is resets the previously assigned key binding to the HelloWorld procedure.
I hope that I have been of help.

VBA to minimize ribbon with SendKeys

As described in the solution here I created the following VBA to minimize the ribbon menu in Excel:
Sub Minimize_Ribbon()
SendKeys "^{F1}"
End Sub
However, when I run this VBA it opens the following link to the Help section from Microsoft:
https://learn.microsoft.com/de-de/office/vba/api/overview/language-reference?redirectedfrom=MSDN
I assume this issue is because somewhow the ^ which should run the Ctrl is not working in the VBA.
What do I need to change to make it work correctly?
As already described in the comments the solution is:
Sub Minimize_Ribbon()
CommandBars.ExecuteMso "MinimizeRibbon"
End Sub

1004: Application-Defined or Object defined error after switching from Excel 2013/2016 to 2010

it's a very simple macro and I read tons of threads, none of which resolves my case. I get the .Caption line highlighted. I tried to Dim the form as new form and it does not help as well.I have a form which I want to call from a button which is apparently not done correctly from my side.
Sub BtnAdd_Click()
frmAddCB.Caption = "Add Transaction"
frmAddCB.Show
End Sub
Cheers,
When executing this step by step with F8, just before the line that was highlighted on my standard module when executing with F5, it jumps to the Private Sub UserForm_Initialize() sub which is in the Form code itself. In that Initialize sub I had this line of code which was causing the error in Excel 2010:
TransTypeComboBox.List = Application.WorksheetFunction.Transpose(ThisWorkbook.Names("TransType").RefersToRange))
After I changed this to the old-school way with a loop, it started working properly. Probably something about my dynamic range or something about the RefersToRange is not working the same way after Excel 2010.
For Each TT In Range("TransType")
With Me.TransTypeComboBox
.AddItem TT.Value
End With
Next TT

Sheet select from a Userform button causes data entered to go on previous sheet

I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub

Workbook_Open() won't execute Excel 2011

Using Excel 2011 (should be same as Excel 2010)
Code is under the "ThisWorkbook" module in Excel
Events are enabled
Macros are enabled
I can't seem to get either Workbook_Open() or Workbook_BeforeClose() to execute. I've read numerous posts on the subject but no solution. Here is some simple test code that should execute but doesn't. Any help would be greatly appreciated.
Private Sub Workbook_Open()
ActiveSheet.Range("BL4").Value = "Open is working"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
ActiveSheet.Range("BL5").Value = "Close is working"
End Sub
First make sure you have put this in this in the right place and have macros enabled.
Then, try adding this line to the workbook_open method:
MsgBox "HELLO"
Do you see the msg box? You're choice of cell looks a bit strange
Also, I think you need to use a .xlsm file not .xlsx (Although not sure on that one)
FInally, if a plugin calls something like this line, it could cause your events not to fire..
Application.EnableEvents = False
So make sure you have tested it with no other sheets or addins open.

Resources