I have an excel workbook with five different recorded macros on five different sheets. Is it possible to create another macro and run those five macros consequentially?
The difficulty is those macros are not specified to each worksheet. I have to run those macros each time manually.
Any thoughts are appreciated! Thank you.
As explained in this question: Multithreading in VBA cannot be done natively.
Can't be done natively with VBA. VBA is built in a single-threaded apartment. The only way to get multiple threads is to build a DLL in something other than VBA that has a COM interface and call it from VBA.
So running all 5 macros at the same time would require a lot of work.
But OP mentioned in the comment that running all 5 macros sequentially would be an option.
What you can do is:
Add a new module
Add a new public sub in this module
Reference all macro names in this sub
Example of how this macro could look like:
Public Sub allMacros()
macroName1
macroName2
macroName3
Sheet1.macroNameNotUnique
Sheet2.macroNameNotUnique
End Sub
Now running this macro will run all the specified macros sequentially.
Related
Looking for a code to activate a function that i made called "IV" at 9:00 pm every day.
i tried following some other scripts but i can get it to activate the function using clock time.
I assume you're using windows.
You could use a sleep function to control the process, but when you run macros you can't access your sheets in excel. VBA is not really set up for this type of thing so I propose another method.
Open up visual basic and put this in Thisworkbook:
Private Sub Woorbook_Open()
yourmacro.IV()
ThisWorkbook.Close()
End Sub
Where you just substitute the names of yourmacro with the name of the macro file which contains your prodedure called IV.
This will run when the workbook is opened by the task scheduler, run the procedure, then close the workbook.
Then make a new task using windows task scheduler that opens this workbook at 9pm every night. (tutorial here: https://datacornering.com/how-to-run-excel-file-from-windows-10-task-scheduler/)
This question already has an answer here:
Can I assign a function in an excel add-in to a button in the workbook?
(1 answer)
Closed 3 years ago.
I would like to run a macro defined in an AddIn from a button on an excel sheet. I tried to enter the name of the macro in the "assign macro" dialog box that appears when right clicking the button (I don't know the exact English title of the dialog as I'm working with a German Excel version) in the formats
AddInName!MacroName and
'AddInName'!MacroName.
In both cases the name of the AddIn is stripped away automatically when I reopen the dialog.
The format "'AddInName'!MacroName" was rejected as "a too complex formula"
On the machine on that I am developing the AddIn this works but on another machine where the AddIn is running without problems this particular function does not work.
Is there a way to access a macro inside of an AddIn that may be located at different locations on different machines (C:/user/"user_name".../AddIns) ?
Long story, made very [long]: maybe.
You'll have to work out the file sharing and permissions between the two computers but you can use an AddIn on a shared network drive so it is entirely possible.
I would caution against using the same AddIn simultaneously between computers. It would be easier, safer, and more stable to make a copy.
You're on the right track with your string creation, but rather than adding it in the macro selection box, add it to the OnAction property of the button in the worksheet's code module.
I'm going from memory so I might be mistaken but I'm pretty sure your string will be built like this...
"'[addin.xlam]!project.module.macro'" \edit: tested and confirmed not to work
Note that there are there ticks, it is an outer "double quote" encapsulating an inner 'single quote'
You can also pass arguments to the macro with an appropriately built string too, it's relatively easy when using variables or integers but it gets tricky when you start using string literals.
Just finished my post to read this:
"#Peh: That might work, but then the excel sheet has to be an xlsm file what I wanted to avoid. – chrmue 9 mins ago"
Obviously that rules out my entire solution.
I spent several hours on this and all I've done is run around in circles.
The comments in my code appear to be wrong, and while I all passing parameters in my working code, I am only doing it within the same workbook and have failed every attempt to send them to another workbook via OnAction and even Application.Run
It may be a fools errand but I'm convinced it's possible so in but giving up.
I have a button on the sheet:
Shapes.Add xlButtonControl
It passes two arguments through a procedure:
OnAction = "'passthroughme ""passing"", ""through"" '"
But hithertofor, that's as far as it goes. No success reaching external workbook's while also including arguments.
I also have several examples of existing code that works just fine, but I haven't been able to get any is them to pass arguments to an external workbook:
Select Case Application.Run(testingFunction)
Application.Run procToCall, procArgs
testResult = Application.Run(testingFunction, testingArgs)
Application.Run "extractPDF." & loPDFvendors.ListColums(gTCN_PDFMAP_PROCLIST).DataBodyRange(mSupplierIndex), ws
Not giving up yet, but I am defeated for the moment
I want to create a macro that takes the user to the last previously active sheet. I need this macro to keep track of all previously active sheets, which it should be able to do automatically. I want to add this to an Excel add-in, since it needs to be compatible with all the worksheets that my company's using. The sheets themselves are not macro-enabled, so I can't have any code stored in the modules of ThisWorkbook.
I have tried out the examples that I've found on other websites. Unfortunately, all of them require using ThisWorkbook, which I won't be able to use. Is there a way to do this purely within an Excel add-in?
Public MyPrevSheet as String
Sub GoToPreviousSheet()
If Len(MyPrevSheet) > 0 Then
Sheets(MyPrevSheet).Activate
ActiveWorkbook.ActiveSheet.Previous
Else
MsgBox "You have not switched sheets yet since opening the file!"
End If
End Sub
Ideally, I would like for MyPrevSheet to automatically track the last active sheet in the file.
Update: I managed to make it work using Application Events. The idea is to create a class module that can then be assigned the Application.
Here are some useful links that helped me: http://www.cpearson.com/excel/AppEvent.aspx
https://www.jkp-ads.com/Articles/buildexceladdin05.asp
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.
I want to create an excel template that will includes formulas for dating the columns. However, since those formulas will be based on TODAY(), I need to convert them to static strings (so the dates don't change everytime someone opens it). Is there a way to add a macro that will run automatically when someone creates a new spreadsheet based on the template? (Similarly to Auto_Open(), only on Create, rather than Open). If so, I could just create a macro that will replace the formulas with their results upon document creation. Can this be done?
[Note: I'm not married to this solution; it just seemed like the simplest way to protect my spreadsheet. If someone can suggest an alternative approach, I'd be obliged.]
I have a couple thoughts...
If you run a copy/paste values macro every time it really won't matter, right?
You could check if the file exists yet (has been saved), and if not
then this must be the template opened as a new workbook, maybe?
Code:
Private Sub Workbook_Open()
If Dir(ActiveWorkbook.Name) = "" Then
'run the macro?
MsgBox "I'm gonna run this macro"
End If
End Sub
You could have a cell on one of the sheets, that will never be used,
or is hidden, that will store whether or not to run the macro, and
change that when the file is opened or when the macro is ran. Then
have a macro run on open that checks that cell. (Or custom/document property)
You could populate the cells that have the today() formula only on
open and if they are already populated then don't run the macro?
I realized that there is no need for a Workbook_Create() function, as that behavior can be emulated by simply deleting the macro after it has run once (which happens when it is first created). Deleting macros is done automatically when the file is saved with a .xlsx extension. In addition, you need to prevent the macro from running when you open the template itself (while editing the it). You can hold the SHIFT key when opening it to prevent auto-run macros, but since that method isn't foolproof, I added this code at the top:
Private Sub Workbook_Open()
'If we're opening the template, don't run the macro
If Application.ActiveWorkbook.FileFormat = xlOpenXMLTemplateMacroEnabled Then
Exit Sub
End If
...
'add code here to SaveAs .xlsx, thus removing the macros, so it won't run every time.
End Sub
(Note: I didn't show my SaveAs code as it is rather messy: I wanted to suppress the default warning about losing macros, but wanted to also protect the user from inadvertantly overwriting previous file. If anyone is interested, I could post it)