Automatic Macro for every sheet - excel

I have a method which is just formattong the excel sheet but the problem is i have 14 sheets and auto_open method is working in a Sheet1 only?
is there any way to a run a method which will run for everysheet automatically when someone just open the excel?

The best would be to create function or a sub which is called by every "OnOpen" events of your sheets.

A for loop for all the sheets on workbook in Auto_open method.

Related

Open Excel worksheet - run time error with other macros

I made a workbook with several sheets for data entry, and I want the index/main sheet to display upon opening every time.
Everything on the internets says to use something like below, but I get a runtime error.
I've made sure the worksheet is named "Attendance Main" and the spelling is correct.
Most troubleshooting issues I've seen are about activating sheets for copying/pasting and other actions. I just want the main sheet to display each time the workbook is opened.
When I create a new workbook, with 2 basic sheets, the macro works. So I am wondering if other macros are interfering with it. However, none of my macros are in "ThisWorkbook", most are in their own modules.
Any help appreciated, and if you need any info from me to narrow down the issue I will try to provide what I can.
Private Sub Workbook_Open()
Worksheets("Attendance Main").Activate
End Sub
Run-time error '1004'
Activate method of Worksheet class failed
For me a simple select code inside the open event of Workbook works fine.
Private Sub Workbook_Open()
Sheets("ShowThisFirst").Select
End Sub

VBA events for worksheets that are macro generated

I relatively new to VBA. Here is what I need to accomplish:
I need code to run when the value of some cells changes in some worksheets. I just learned how to do this using events.
The problem I'm having is that the worksheets that need the event are not there to begin with, but are generated by a macro. Also, their number might vary depending on the data (i.e. sometimes the macro might generate 3, sometimes 10.. and will rename them using some cell values, so also the names differ every time).
From what I can see, the event that triggers the macro on cell change needs to be coded for the object itself (specific worksheets). Is there a way to do it for worksheets that are macro-generated, and don't exist in the workbook when first opened?
Any help would be much appreciated!
A tiny trick:
The usual way to add a sheet is to use code like:
Sheets.Add
which will get you a fresh new sheet, but this new sheet will have no event code associated with it.
Instead create a template worksheet (called templ) and in this template worksheet include all necessary event macros. Now, instead of Adding a new worksheet, just:
Sheets("templ").Copy after:=Sheets(Sheets.Count)
you get a new worksheet, but with all macros attached!

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.

Programming a macro to run other macros on each worksheet

I have an excel workbook with many different sheets. Each sheet has a single macro in it wich saves the sheet as a pdf and opens outlook with that pdf as an attachment. I am looking for a function to place on one worksheet that will run each macro without having to manually run each on their respective worksheets.
Say there is a macro in each worksheet called SheetMac. Put the following in a standard module and run it:
Sub MASTER()
For Each sh In Sheets
sh.Activate
Call sh.SheetMac
Next sh
End Sub

Run a macro in a specified worksheet from another workbook

Here's my problem: I have two workbooks, one is mine, say Projet.xlsm, and the other is not, say Query.xlsm. Query.xlsm is shared between me and a lots of people in my company so I can't modify it.
My project.xlsm aims to plot some datas extracted with Query.xlsm. My job is to make it an automated process.
How Query.xlsm work: on the first sheet there is some options to select; then we have just to click a button on the first sheet. Finally all the datas are extracted and appears in second sheet.
I've built a macro in project.xlsm wich open Query.xlsm and select the options I want.
Now I'm tryi,g to "click the button" (sheet1 in Query.xlsm) from my macro in my project.xlsm. I've tried things like this: running excel macro from another workbook
But I think I have to mention the name of my sheet somewhere.
Thanks in advance
Assuming you have the workbook open (which it sounds like you do), this worked for me even on a Private CommandButton Sub.
Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = True
Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = vbClick
You may create a public procedure in Query.xlsm Workbook in first sheet.
cmdClick refers to the name of activex command button. Below is sample code.
Public Sub cmdClick()
CommandButton1_Click
End Sub
You can call this procedure from project.xlsm using below syntax.
Workbooks("testing.xlsm").Sheets("Sheet1").cmdClick

Resources