Running an existing Macro on a new Excel File automatically when opened - excel

(Excel 2010 question)
I have a reporting application that exports To excel. I have a Macro created that does some functionality. I need to run this macro whenever I open this new Excel Workbook after I export it.
Since it's a brand new worksbook, the excel does not have the macro that I have created embedded into it. So need to know if this is possible and how to do it.
Thanks!

You can open both the workbook that contains the macro and then the NEW workbook.
Then hit F11 for the Developer's Console. Usually it opens with the Project Explorer open on the left. If it isn't you can open it in the View menu, or by hitting Ctrl+R.
You need to copy the module that contains the macro into the modules of the New Workbook. Either insert a module from the Insert menu, or right click on the Modules > Insert > Module, then copy and paste the code, or you can just INSERT the module.
Let's assume your original Macro is named "Macro1". Make sure it isn't private.
I made one for example:
Sub Macro1()
MsgBox("Welcome to the Workbook!")
End Sub
In the Project Explorer, select the New workbook, and there should be an object titled "ThisWorkbook". Right click on that and select "View Code".
Private Sub Workbook_Open()
Call Macro1
End Sub
Save it, exit, open it back up and see your macro execute.

Related

How to open a blank workbook automatically after hidden startup workbooks are added?

Background: I have created an alternative startup location with two files in it on the network (one .xslb and one .xla), for the purpose of macro storage, and distribution.
Problem 1: When I open Excel from the shortcut there is no blank workbook.
Problem 1 Solution: A blank drawing opens when I open Excel from shortcut. Code Shown below added to ThisWorkbook Excel Object of startup file.
Option Explicit
Private Sub Workbook_Open()
If Workbooks.Count <= 2 Then
Workbooks.Add
End If
End Sub
Problem 2: I now open a blank drawing every time I open excel even when I double click RandomName.xlsm, I would like to suppress this. I understand that the code will run on the startup workbooks, condition will be true, open the blank book, and then opens RandomName.xslm.

Export sheet including VBA and a command button

I need to save several Excel sheets to separate files.
The sheets are protected and locked, however I want to make spellcheck available.
This is possible with VBA using a small routine to unlock>spellcheck>relock
Sub SpellCheck()
ActiveSheet.Unprotect
Cells.CheckSpelling CustomDictionary:="CUSTOM.DIC", IgnoreUppercase:=False, AlwaysSuggest:=True, SpellLang:=1033
ActiveSheet.Protect
End Sub()
Now I placed command button on the sheets I want to export and assigned my spell check macro.
I save the files with vba as XLSM
Sheets("exportsheet").SaveAs Filename:="mysheet.xlsm", FileFormat:=52
If I click on the button in the newly saved file, the macro is linked to the original source excel which will open. The assigned macro link looks something like this: original_excel.xlsm!spellCheck()
How can I export a sheet including the VBA code which is assigned to a command button in a way that the macro is not assigned to the original workbook.
Any thoughts on that?
If you want the worksheet to be self contained after you export it from the workbook, make it self contained to start with.
Place all routines that are accessed from the sheet into that sheet's code module (instead of into a shared standard code module).
This way the sheet has no dependencies and will be self contained once exported to a new workbook.

Enabling macro by default on a workbook

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.

exec a xlam everytime I open an excel file

I have the following peace of code to exec a xlam file when I open the excel file:
Sub Auto_Open()
Application.OnTime Now + TimeValue("00:00:05"), "readCsv"
End Sub
Sub readCsv()
....
End Sub
I add it as Add-ins so every file I open has the xlam file on it. If there is no other excel file opened, it works perfectly but after that without closing the excel file I open a second one it does nothing. I need to exec the code even if there is another excel file opened. Is that possible?
I also try to do it writing the code in ThisWorkbook but the result is the same, If there is another excel file opened it does nothing.
To use your macro in any of the worksheet opened (irrespective of name) you can only possible do it by using a personal workbook Here is the Link
If the above doesnt suits you can prepare Add-ins and install it for users (Google for it if this is the case)
EDIT:
How to get add in in all opened files....
Goto File - Option - Quick Access Toolboar - From the dropdown "Choose Command from" - select Macros then select add-in macro - Add it, Below there would be a modify button Select the icon you like from it - OK
Now you will be able to see the icon with the addin function linked on top of the excel - click it for functioning, it will remain there forever ( if missed you can reapply the settings)....
The procedure are for 2010, for 2007 it should be similar....for 2003 there is a different way to achieve it...
Copy the .xlam to C:\Users[user]\AppData\Roaming\Microsoft\Excel\XLSTART. It will load every time you open Excel.

VBA Run-time error '9': Subscript out of range; trying to activate another workbook

I'm trying to create a VBA in Excel 2010 that takes info from another spreadsheet that I'm not allowed to alter and bring it over to the spreadsheet with my macro built in. Here's my code:
Sub BringUpWorkbook()
Workbooks("RITE 1624.xls").Activate
End Sub
I have several VBA books, have visited dozens of sites on the Internet, including those here at stackoverflow.com, and cannot find a reason why I'm receiving the run-time error. The workbook is already open, I've tried adding everything trailing the title, I've tried removing the .xls, I've even did all of the above with in a variable. Any ideas?
Make sure the extension is correct. If it's a excel-2010 file like you indicate, you may need to update your code to reflect the extension of your "RITE 1624" file (e.g. .xlsx for a 2010 Excel workbook, .xlsm for a 2010 Excel macro-enabled workbook, or whatever the extension is.
Sub BringUpWorkbook()
Workbooks("RITE 1624.xlsx").Activate
End Sub
EDIT:
To make sure you have the right name of the workbook, you can print the name of each workbook in an immediate window.
Open up the VBA editor, and then press Ctrl+G to open the Immediate Window (or do View > Immediate window). Then run the following Macro:
Sub OpenWkbkNames()
For Each Workbook In Workbooks
Debug.Print Workbook.Name
Next
End Sub
Note that this will give you the names of all the open workbooks in the same Excel instance as your macro. If your RITE 1624 file is in a separate Excel instance, then the instance that your macro is in will not be able to see that file (and it won't appear in the output of the OpenWkbkNames code above). The simplest way to resolve this is to open all of your necessary files from the Excel instance that contains your macro.
Sub BringUpWorkbook()
Workbooks.Open("RITE 1624.xls").Activate
End Sub

Resources