Running excel macro on multiple files - excel

I have an excel spreadsheet with a very complex macro. The spreadsheet takes a file (imported through a button on the sheet), and runs statistics on it. is there any way to automate the macro to run on multiple files in a folder?
Thanks in advance

The macro identifies the worksheets from which it draws data and these sheets are in a workbook, which is also identified. Hence, by changing the name of the source(s) in the code, perhaps even dynamically, you can make the same macro perform its magic on other workbooks.
With that said, since VBA absolutely needs a workbooks to identify a worksheet and must have a worksheet in order to identify a cell, VBA will provide a default for either if the code doesn't mention another. Therefore the innocent Cells(1, 1) or Range("A1:B10") you may see in your code in fact stand for ActiveWorkbook.ActiveSheet.Cells(1, 1) or ActiveWorkbook.ActiveSheet.Range("A1:B10"). Therefore, if you want to change the default workbook for another the process is to first introduce a variable to specify the workbook and worksheet and then change the object assigned to that variable.
In a less generic way, let me presume that you don't have syntax like Range("A1:B10") in your code at all but Worksheets("Sheet1").Range("A1:B10"), identifying the worksheet but not the workbook. Let me further presume that the sheet names are the same in all the workbooks on which you want to run the code. In that case you would make the change as shown below.
Dim Wb As Workbook
Set Wb = ActiveWorkbook 'or perhaps ThisWorkbook (=the one having the code)
' and then change all applicable instances to
Wb.Worksheets("Sheet1").Range("A1:B10")
Now you can change the code to specify another workbook simply with:-
Set Wb = Workbooks("My workbook.xlsm")

Related

Invalid use of property Excel VBA

Sub test()
Worksheets("Sheet1").Range("A1").Value = 20
End Sub
This simple code is giving error when I compile it.
activesheet. works fine.
I want to know whats resulting in an error and how to fix it...
looks like it's not identifying the sheets, workbook etc.
The answer depends on which error you get. There can be 2 issues:
1. Workbook not specified
You have more than one workbook and Excel is looking in the wrong workbook for your sheet named "Sheet1", then you need to specify the workbook.
Workbooks("my-workbook").Worksheets("Sheet1").Range("A1").Value = 20
or if it is in the workbook where the code is running at it is better to use
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = 20
Note that you should avoid ActiveWorkbook which is not very reliable.
2. Wrong worksheet name
There is no worksheet named Sheet1. Check your worksheet names. Note that there are different ways to specify a worksheet.
Specify by number
Worksheets(1).Range("A1")
This uses the position of the worksheet in the tab bar below the worksheets. Note that is not very reliable because position can easily be changed by moving the tabs around.
Specify by tab name
Worksheets("Sheet1").Range("A1")
This is probably the most common method. The worksheet is specified by its tab name. This is more reliable than by number.
Specify by VBA name
Sheet1.Range("A1")
Here the VBA name of the sheet is used. This name can only be changed in the VB editor and is not visible to the user, and has nothing to do with the tab name. Using this ensures that the VBA code still works on the desired worksheet even if a user changes the tab name of the worksheet.
So if the tab name is Sheet1 its VBA name can be Sheet5 and it can be on position 3 in the tab bar.
Using this example …
Worksheets("Sheet1").Range("A1")
Sheet5.Range("A1")
Worksheets(3).Range("A1")
… are all 3 accessing the exact same worksheet just by different names. So better to use meaningful names (and no numbers) here to not confuse.

When does the activeworkbook/activesheet change?

I have some questions about the activeworkbook/activesheet properties, would appreciate your help:
In any instances, there would be at maximum only one activeworkbook or no, and that applies to activesheet. Is that right?
If properties like ranges,cells,... are not fully qualified when used, they are by default meant to be that of the activeworkbook.activesheet. Is that right?
Will the activeworkbook change to another workbook by vba actions other than activating the other workbook?
When I use wb.ws.range(cells(x1,y1),cells(x2,y2)), are the cells implied to be the cells of wb.ws? Or do they need to be qualified?
Is it possible to run the vba codes stored in the modules of a workbook without activating the workbook?
Thank you.
In any instances, there would be at maximum only one activeworkbook or no, and that applies to activesheet. Is that right?
Yes. .ActiveWorkbook returns a Workbook object that represents the workbook in the active window (the window on top).
If properties like ranges,cells,... are not fully qualified when used, they are by default meant to be that of the activeworkbook.activesheet. Is that right?
Yes
Debug.Print rng.Parent.Name '<~~ This will give you the name
Will the activeworkbook change to another workbook by vba actions other than activating the other workbook?
Yes it is possible. For example if you send your Active Workbook to Back or minimise it then if the next window in zOrder is a workbook then that will get activated
When I use wb.ws.range(cells(x1,y1),cells(x2,y2)), are the cells implied to be the cells of wb.ws? Or do they need to be qualified?
Only if ws is active then yes else they will refer to Activesheet and hence it is always good to fully qualify your objects
With ws
Set Rng = .Range(.Cells(x1, y1), .Cells(x2, y2))
End With
Is it possible to run the vba codes stored in the modules of a workbook without activating the workbook?
Yes. You can use Application.Run

ISO How to program ActiveX button in Workbook 1, to run macros in Workbook 2?

Situation:
I have two workbooks:
Workbook #1: A Downloaded data set from online data repository
Workbook #2: A Master collection of Macros
I have built a collection of Macros to format a data set after it's been downloaded from an online repository. This data set can differ greatly based on features that users have chosen before downloading the data. The macros I created cover all possible scenarios. Thus, when opening the View Macros dialog box there are an overwhelming amount to choose from. Even with efficient naming conventions it's too much to sift through for my audience, who has an average to low experience level with Excel.
In order to simplify their experience, I wanted to level the playing field by simply providing a "Go" button after they choose from an ActiveX dropdown list.
I successfully created the dropdown, populated the list, and upon activation of the "Go" button, the selection triggers a specific Macro to run.
MY PROBLEM:
I need the Subcode on the "Go" button in Workbook #2 to force the macros to run in Workbook #1.
Thank you ahead of time for all your help!
You can take cues from the macro recorder.
Here is some basic code that is stored in one workbook and manipulates cells in another workbook.
Sub Macro2()
Dim mlib As Workbook
Dim wb As Workbook
Dim ms As Worksheet
Dim ws As Worksheet
Set mlib = ActiveWorkbook ' the file with the macros
Set ms = mlib.Worksheets("Sheet1")
' write into the macro workbook
ms.Range("B3").FormulaR1C1 = "asdf"
'activate another workbook that is already open and is called somefile.xlsx
Windows("somefile.xlsx").Activate
' set variables to reference that workbook
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
' write something into somefile.xlsx
ws.Range("B1").FormulaR1C1 = "copy me"
'copy something within somefile.xlsx
ws.Range("B1").Copy ws.Range("B2")
' copy something from the macro workbook to the somefile workbook
ms.Range("A1").Copy ws.Range("A5")
' activate the macro workbook
mlib.Activate
End Sub
I asked for some sample code so I could explain in your context.
There are about a dozen different ways how you can identify the "other" file instead of hard-coding it into the macro. If you could be bothered to provide a bit more information, that little detail could also be taken care of.

What to do to avoid VBA function executed in the wrong work book

I have several excel files that have timer and macros executing. But a big problem is when workbook A's macro is called, while workbook B is active. The macro is executed in the wrong book and failed.
Do I need to put windows().active at the beginning of every function?
If I have different modules how do I pass this workbook name to all of them?
This seems excessive and not right. Is there any good solution to this problem?
Looking forward to your answers
You are on the right track with this
2.If I have different modules how do I pass this workbook name to all of them
I assume that your macro is using the ActiveWorkbook property, or just using Worksheet properties like Range without qualifying them?
Instead of using ActiveWorkbook use ThisWorkbook. Instead of using Range use ThisWoorkbook.Worksheets(1).Range and so forth. Otherwise the macro will assume that the active worksheet is the one you want.
Sub MyMacro
Range("A1").Text = "Test"
End Sub
Try
Sub MyMacro(ByVal oWorksheet as Worksheet)
oWorksheet.Range("A1").Text = "Test"
End Sub
Then pass the worksheet object as a parameter.
You may also find the ThisWorkbook object useful - it is the workbook the macro resides in, or the Application.Caller object, which is the object calling the current macro, for example the Range object if it is a cell formula, or presumably the timer object in your case.
If your macros behave the way you described it, they probably depend explicitly or implicitly on
ActiveWorkbook
or
ActiveSheet
Those kind of dependencies should be avoided, if possible. The macro recorder produces such code, you should change it immediately whenever you have recorded a macro.
For example, if you have some code like
s = Range("A1").Value
Excel implicitly changes that to
s = ActiveSheet.Range("A1").Value
One can avoid that by accessing all cells, ranges, workbook parts etc. by explicitly using the right sheet or workbook object:
Dim sh as Worksheet
Set sh = .... ' Initialize sh the first time where the sheet is created or loaded
'later on:
s = sh.Range("A1").Value
By using a parameters of the form
sh as Worksheet, wb as workbook
for your subs and functions, you can pass the right sheet and workbook between modules, which answers your second question. And if you need access to the workbook where your macro resides, use ThisWorkbook.
I'd go one further... make sure your code doesn't have
Selection
or
ActiveCell
objects within them. You would need to rewrite these using the Range object.

Excel VBA - Use Module in Workbook B to Update Data in Workbook A

I have 10 XLS's, each of which contain a a few hundred lines of VBA that do the same thing. I want to move this common code into an 11th XLS, and have the other 10 call the code in the 11th XLS. The common code must have access to all of the data and worksheets in the calling XLS. This last requirement does not seem to be addressed by other answers to this question on SO. Can I pass the calling XLS's worksheets in as a parameter, or something similar?
Instead of putting this into a secondary XLS file, I'd recommend creating an XLA file (an Excel Add In).
This is the exact scenario for which XLA was intended. XLA will work the way you intend in this case.
For details on creating an XLA, see this page.
Yes, you can pass references to workbooks, worksheets, ranges, etc. as parameters to any function:
Public Sub CallMe(ByVal oWorkbook as Workbook)
Dim oWorksheet as Worksheet
Set oWorksheet = oWorkbook.Worksheets(1)
' Do stuff...
End Sub
Note that you'll probably have to re-write a lot of the code you copy from the 10 workbooks since they'll be full of implicit references to "this" workbook, such as Worksheets(1) etc. As in the example above, you now need to say oWorkbook.Workbooks(1) instead.

Resources