Enabling macro by default on a workbook - excel

I am new to VBA/Excel programming, I am refreshing a pivot table using a macro. I have the following VBA code on one of my sheets.
Excel version : 2013 ( saving my file as macro enabled workbook)
Private Sub Worksheet_Activate()
RefreshPivotTables
End Sub
Private Sub Worksheet_Deactivate()
RefreshPivotTables
End Sub
The code works fine when I switch back and forth after enabling the macro, but when I open the Excel doc I get this "Enable macro ribbon" at the top which is preventing the macro from running. I need to switch to another tab and come back to my original sheet to activate the macro.
Is there a way to set "Enable Macro" for the workbook by default / some code that will do this on behalf of the running user? I cannot expect all my users to switch workbooks and come back to see the refresh happen.

Go into the options in Excel and then to the trust center settings. Put the location in as a trusted location.
Alternatively on my machine if I open a sheet, enable macros then close it again a few times Excel asks if I want to make this a trusted document.

Related

Custom Tab Order macro and functional disabilities when second Excel Workbook opened

I have a series of document templates created in Excel macro-enabled workbooks with an array of different macros coded in vba. I'm fairly new to vba, but I was able to cobble together code from several different sources that mostly achieved what I was looking to do: create a customized tab order, which also incorporates ActiveX Comboboxes.
For the most part, all code in each modules works great on the respective files saved locally on my machine. However, if these files are Emailed or saved on an intranet site or somewhere where they must be downloaded by end-users, something squirrelly happens that I haven't been able to sort out. When the first file is opened, the user will receive Excel's "Protected View" prompt, requiring that they click the "Enable Editing" box to access/modify the file. This is fine and the user can manipulate the first xlsm file without issue. However, with that first xlsm file open, if they additionally open any of the other xlsm workbooks that I've created (with ostensibly identical code), they are not prompted with the "Protected View" message and the following happens in that second workbook:
The cell focus is redirected to cell A1 instead of whichever cell should have been selected when the second workbook was opened,
The customized tab order I've delineated is disabled,
Both the horizontal and vertical scrollbars vanish from view, and
All tabs for each sheet in the second workbook are no longer visible.
I've tried manipulating and rewriting the code in each of these workbooks every which way I could think of, but I can't figure out what could be the culprit causing the above issues. I'd wager it has something to do with how Excel is initializing the second workbook when it's been downloaded, and that it has more to do with the code in the ThisWorkbook module -- here are the snippets that I think are relevant:
Private Sub DisableTabbing()
If Not (Application.ActiveProtectedViewWindow Is Nothing)
Then Application.ActiveProtectedViewWindow.Edit
End If
Application.OnKey "{TAB}"
Application.OnKey "+{TAB}"
Application.OnKey "~"
Application.OnKey "+~"
Application.OnKey "{ENTER}"
Application.OnKey "+{ENTER}"
End Sub
Private Sub Workbook_Activate()
EnableTabbing
End Sub
Private Sub Workbook_Deactivate()
DisableTabbing
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
EnableTabbing
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
DisableTabbing
End Sub
On my machine, I don't encounter this at all when any two workbooks are locally saved and opened, but only when the files are Emailed/downloaded. I'm sure there's a relatively simple fix for this, but I'm not as experienced with VBA, so I'm hoping someone here might be able to point me in the right direction. Is there something in the code that could be causing that, or better yet, some additional code that I could insert that would prevent it from happening?

What are the differences between the microsoft excel security notice popup and the message bar in excel 2013?

I am encountering an issue developping a VBA functionality in an excel Workbook.
This workbook is geared toward being used by end-users without administration rights, who need to activate the macros on this specific workbook before being able to use it.
To this end, I have done the following:
I created a "temporary home worksheet" which asks the user to activate the macros (no code, a simple message in the cells of this worksheet), which is initially visible
I created a "home worksheet" which is initially (very) hidden, and which holds buttons and other controls that can be used
I wrote a simple sub which is called:
when the workbook is opened and the activesheet is the "temporary home worksheet"
or when the "temporary home worksheet" is activated
This sub does the following (see code below):
hide the "temporary home worksheet" (to very hidden)
show the "home worksheet"
This enables to:
show a message asking the user to activate the macros if he/she has not done it
redirect to the target "home worksheet" if the user activate the macros while the "temporary home worksheet" is activated
redirect to the target "home worksheet" if the macros are activated and the user activates the "temporary home worksheet"
' Code to hide temporary ws and show target ws
Private Sub setTargetVisibility()
Dim ws As Excel.Worksheet
ThisWorkbook.Activate
Set ws = ThisWorkbook.Sheets(getParm("tempHomeWSName"))
With ws
.Visible = xlSheetVeryHidden
End With
Set ws = ThisWorkbook.Sheets(getParm("homeWSName"))
With ws
.Visible = xlSheetVisible
.Activate
End With
End Sub
This works fine most of the time, because the users get prompted to activate the macro through the message bar:
However, they sometimes get another prompt to activate the macros: a microsoft excel security notice popup. Whenever they activate the macro through this popup, they get a runtime error 1004 (e.g. the method Activate of the _workbook object failed), in the sub with the code above.
Edit: an important thing to note is that this excel file is opened in Protected view as it is generated by a web application and downloaded by the user before use.
What cause these different way of enabling macro in excel 2013 (message bar vs. popup)? What are the behaviour differences between them?
Thanks in advance.
Regards,
By default, when you first open a macro-enabled workbook you’ll see a yellow “SECURITY WARNING” bar appear just underneath the ribbon. Clicking the “Enable Content” button will enable macros.
This will trigger any macros that run when the workbook is opened, so don’t click this by mistake!
If the Visual Basic Editor is open at the time you are opening the file with macros, the Microsoft Excel Security Notice will be displayed.
If you trust the source of the file and know that all the macros are secure, click the Enable Content or Enable Macros button. This will turn on the macros and make the file a trusted document. The next time you open the workbook, the security warning won't appear.
If the source of the file is unknown and you don't want to enable macros, you can click the 'X' button to close the security warning. The warning will disappear, but macros will remain disabled. Any attempt to run a macro will result in the following message
As mentionned by #David Donayo in the accepted answer, what triggers the Microsoft Excel Security Notice popup vs. the message bar warning is the fact that the VBA Editor is already open for another xlsm file.
What caused my issue (runtime error 1004 when trying to make Object Model calls such as Application.Calculate or ThisWorkbook.Activate at WorkbookOpen event) is that the issue described here (https://support.microsoft.com/en-us/help/2745652/object-model-calls-may-fail-from-workbookopen-event-when-exiting-prote) is still present in excel 2013 when the Protected View is exited and then the macros are activated through the Microsoft Excel Security Notice.
This issue is not raised when the Protected View is exited and the macros are activated via the message bar warning.
I did not implement the workaround described in the link above (defer Object Model calls from the WorkbookOpen event to the WorkbookActivate event if the workbook is opened in Protected view) as in my context end users should not have the VBA editor open. Therefore I cannot tell whether this workaround works, but see no reason why it should not.

Workbook_Open() in Excel 2016 not firing

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all!
The simple event sub
Private Sub Workbook_Open()
MsgBox "Work book is open"
End Sub
does not seem to work. However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.
I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window. Is this a bug in 2016 and is there a workaround?
I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there. But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.
It is a simple single Sub in the ThisWorkbook module. Macros are enabled. In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros. It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function.
Try encapsulating the call with a new instance of Excel. Example below:
Sub OpenInNewExcel()
Dim Background_Excel As Excel.Application
Dim pathName As String
Dim fileName As String
Let pathName = "Enter your path here" 'include "\" at the end
Let fileName = "Enter your file name here"
Background_Excel.Workbooks.Open fileName:=pathName & fileName
Background_Excel.Parent.Quit ' This is how you close the file completely using VBA otherwise the file will close and the Excel Shell will remain.
End Sub
Also make sure that enable macros is turned on in the Options-Trust Center.
You have to add the file/folder location of your workbook as a "Trusted Location".
You can see more info about that in Designate trusted locations for files in Office 2016.
I have same problem then I found solution after google it:
https://www.myonlinetraininghub.com/excel-forum/vba-macros/excel-2016-workbook_open-event-doesnt-trigger
Then I also use "Private Sub Workbook_Open()" and "Public Sub Auto_Open()" open in excel 2016 that work fine:
Private Sub Workbook_Open()
CustomStartUp
End Sub
Public Sub Auto_Open()
CustomStartUp
End Sub
Private Sub CustomStartUp()
MsgBox "Work book is open"
End Sub
I've had this problem (I'm using Microsoft 365), and I found this thread.
It happens for me sometimes when I have another workbook already open, then, on trying to open my macro-enabled workbook, before any sheet is displayed I get the Microsoft warning message about macros. Then, although I click its 'Enable' button, the Workbook opens, macros do get enabled, but Workbook_Open doesn't run.
I've never known the problem to occur if no other workbook is open. (Of course, the user might still get the yellow-backed messages at the top of the workbook, asking them to click the Enable Editing and/or Enable Macros buttons.)
Note that my Workbook_Open just calls another 'workbook-opening' sub in a module to do all the opening processing.
My solution: When my workbook-opening sub is called, it sets a global variable to True to indicate it has run.
I've made it obvious to the user that the problem has occurred, by means of a 'Welcome' sheet with all its cells locked, so the user can do nothing; at this point all other sheets are very hidden. The workbook-opening sub, when it runs, deactivates this sheet and makes it very hidden, so the user never normally sees it, and makes the other sheets visible. But if this screen remains, it instructs the user to select the other workbook, then select this one again. My Workbook_Activate code then runs, and because the global variable isn't True, it calls the workbook-opening sub. If this global variable is True, it does nothing.
To make this work, the Workbook_Close sub makes the other sheets very hidden and the Welcome sheet visible, ready for the next time the Workbook is opened.
Hey presto, the problem is solved.
The Welcome sheet actually has a dual purpose, in that if either of the yellow-backed warning messages are displayed, it will remain and force the user, with suitable instructions, to click Enable Editing and/or Enable macros. If the users aren't au fait with macro-enabled Excel, they will just ignore these and try to carry on regardless.
All this is much easier to implement than to explain. I hope it's clear.
And I hope this might be of help to someone.
I had this issue with one of my files as well. I managed to fix this issue by running Workbook_Open manually in the VBA editor once open and saving the file in another location. The file in the new location should have no issue with auto-running Workbook_Open. If this doesn't work, copy the original file to a new location before manually running & saving.
If the newly saved file does not run Workbook_Open, repair your version of Office.

My userform still show my workbook in macro disable computer

I have a question to ask about my userform. I hide my workbook and just userform will visible. It work perfectly in my computer because my computer is macro enabled. But in macro disabled computer, user still can edit my workbook before they enable the macro security warning.
Private Sub Workbook_Open()
Application. Visible=False
UserForm1.Show
End Sub
This is code I used to hide the workbook. Anyone could tell me how can I guide the workbook even in macro disable computer?
Thank
You could add a sheet telling the user to activate macros, protect it and hide all other sheets. You can also protect the workbook to prevent the user from adding or unhiding sheets. (The latter could also be done by setting them to very hidden).
If you need to show the sheets at some point for user interaction you can hide them again upon saving using the Workbook_BeforeSave event.

Userform in Excel 2013 disappears while switching an active workbook behind

I have faced one problem in Excel 2013. In my project, I have a userform that contains a listbox which lists names of all opened excel books.
When a user clicks on a particular name in the list, the appropriate book is activated. So the userform is always on top and while the user is clicking on the names in the list, he can see all opened workbooks getting activated one by one behind the active userform.
This is the way it used to work in Excel 2010.
Now, after upgrade from 2010 to 2013, if a user clicks on a name in the list, selected workbook gets activated but the userform disappears.
What exactly has been broken in Excel 2013 and what is the way to bypass this problem?
Yes, the default behavior has changed. In Pre Excel 2013, The different workbooks are shown within one main Excel window if opened in the same instance. But in Excel 2013, they are shown in their own window. And because of this a modeless userform will only be visible on top of the workbook that was active when the userform was loaded.
And I don't think this has a fix yet. It is by design and may not be fixed.
#Siddharth, based on what you said, I think I have a solution. When you open a form, it will only show up on top of the ActiveWorkbook. As long as all the code is in the same code thread (not sure if terminology is right) , you're stuck with the same workbook; But if you use the Application.Run or Application.Ontime commands it seems to start it's own code thread, which is independent enough to work. Just Activate the workbook/sheet first, and then Unload the form and remote call the macro that showed the form in the first place.
Unfortunately, you need to use Unload instead of Hide, because of the aforementioned issue; which means you need to save any/all options selected.
Private Sub lstFiles_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ThisWorkbook.Application.Workbooks(lstFiles.Text).Activate
Unload Me
Application.Run "USER_Macro1"
'Application.OnTime Now(), "USER_Macro1"
End Sub
 
Private Sub USER_Macro1(Optional Control) ' As IRibbonControl)
ModelessForm.Show vbModeless
End Sub
I tested it in Excel 2013 and so far it even seems to work regardless of the Screenupdating = false issue seen elsewhere.

Resources