Run a macro for all open workbooks

smashclash

Board Regular
Joined
Nov 24, 2003
Messages
126
Office Version
  1. 365
Platform
  1. Windows
I have a lot of files that need a macro run. Currently, I have to open a workbook, run the macro and then go to the next one.

I'm looking for the code to run the macro on all open workbooks. I'd rather not have the macro open each workbook, perform the changes, and then save. I simply want a macro that runs on one workbook, when its finished it moves to the next one until there are no workbooks left. Is there a simple code to do this? I tried doing some searches and due to my limited VB experience I had trouble deciphering which would work for my situation.

Not sure if this is needed or not but below is a copy of the macro I run.

Code:
Sub YTD()
'
' YTD Macro
' Macro recorded 3/10/2004 by CollaT
'

'
    
   
    
    
    
       
    Sheets("feb").Copy After:=Sheets(13)
    Sheets("feb (2)").Select
    Sheets("feb (2)").Name = "YTD"
    Range("A4:C4").Select
    ActiveCell.FormulaR1C1 = "YTD BALANCE"
    Range("A4:C4").Select
    ActiveCell.FormulaR1C1 = "2004 YTD BALANCE"
    Range("A7").Select
    ActiveCell.FormulaR1C1 = "Days in the Year"
    Range("A8").Select
    ActiveCell.FormulaR1C1 = "Hours in the Year"
    Range("E7").Select
    ActiveCell.FormulaR1C1 = "366"
    
    Range("E14").Select
    ActiveCell.FormulaR1C1 = _
        "=+jan!RC+feb!RC+mar!RC+apr!RC+may!RC+june!RC+july!RC+aug!RC+sept!RC+oct!RC+nov!RC+dec!RC"
    Range("E14").Select
    If Len(ActiveCell.Offset(1, 0)) > 0 Then
        ActiveCell.Copy
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End If
    
     Range("G14").Select
    ActiveCell.FormulaR1C1 = _
        "=+jan!RC+feb!RC+mar!RC+apr!RC+may!RC+june!RC+july!RC+aug!RC+sept!RC+oct!RC+nov!RC+dec!RC"
    Range("G14").Select
    If Len(ActiveCell.Offset(1, 0)) > 0 Then
        ActiveCell.Copy
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End If
    
    Range("K14").Select
    ActiveCell.FormulaR1C1 = _
        "=+jan!RC+feb!RC+mar!RC+apr!RC+may!RC+june!RC+july!RC+aug!RC+sept!RC+oct!RC+nov!RC+dec!RC"
    Range("K14").Select
    If Len(ActiveCell.Offset(1, 0)) > 0 Then
        ActiveCell.Copy
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End If
    
    Range("M14").Select
    ActiveCell.FormulaR1C1 = _
        "=+jan!RC+feb!RC+mar!RC+apr!RC+may!RC+june!RC+july!RC+aug!RC+sept!RC+oct!RC+nov!RC+dec!RC"
    Range("M14").Select
    If Len(ActiveCell.Offset(1, 0)) > 0 Then
        ActiveCell.Copy
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End If
    
    Range("Y14").Select
    ActiveCell.FormulaR1C1 = _
        "=+jan!RC+feb!RC+mar!RC+apr!RC+may!RC+june!RC+july!RC+aug!RC+sept!RC+oct!RC+nov!RC+dec!RC"
    Range("Y14").Select
    If Len(ActiveCell.Offset(1, 0)) > 0 Then
        ActiveCell.Copy
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End If
    
    Range("AA14").Select
    ActiveCell.FormulaR1C1 = _
        "=+jan!RC+feb!RC+mar!RC+apr!RC+may!RC+june!RC+july!RC+aug!RC+sept!RC+oct!RC+nov!RC+dec!RC"
    Range("AA14").Select
    If Len(ActiveCell.Offset(1, 0)) > 0 Then
        ActiveCell.Copy
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End If
    
    Range("A1").Select
    
    
   
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,215,963
Messages
6,127,954
Members
449,412
Latest member
montand

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top