Run macro on all files in a folder

jimmisavage

Board Regular
Joined
Jun 28, 2017
Messages
130
Good day,
I have a folder which has a bunch of spreadsheets in them - we're constantly adding and deleting but they're all from the same template. I don't want to put macro's in these files because it seems to scare people in my work!!

I have found a bit of code, which opens the first file but then gives me a subscript out of range warning before running my codes.

Code:
Sub RunOnAllFilesInFolder()
    
    folderName = "S:\Haemophilia\Reports\Study Updates\"
    If Right(folderName, 1) <> Application.PathSeparator Then folderName = folderName & Application.PathSeparator
    Fname = Dir(folderName & "*.xlsx")

    'loop through the files
    Do While Len(Fname)

        With Workbooks.Open(folderName & Fname)

           ' here comes the code for the operations on every file the code finds
        
        Call Behindthescenes
        Call MainRun
    
        End With

        ' go to the next file in the folder
        Fname = Dir

    Loop
    
End Sub

I figured that this might have been because the macro i'm calling isn't from the newly opened workbook - so i tried this, but get the same error:

Code:
Sub RunOnAllFilesInFolder()
    
    folderName = "S:\Haemophilia\Reports\Study Updates\"
    If Right(folderName, 1) <> Application.PathSeparator Then folderName = folderName & Application.PathSeparator
    Fname = Dir(folderName & "*.xlsx")

    'loop through the files
    Do While Len(Fname)

        With Workbooks.Open(folderName & Fname)

           ' here comes the code for the operations on every file the code finds
        Application.Run "'Study Updates Runner.xlsm'!Behindthescenes"
        Application.Run "'Study Updates Runner.xlsm'!Behindthescenes"
    
        End With

        ' go to the next file in the folder
        Fname = Dir

    Loop
    
End Sub

So maybe there is something wrong with my code? I dont think so, because it's quite simple and it works using it normally:

Code:
Private Sub Behindthescenes()

    Sheets("New").Visible = True
    Sheets("Old").Visible = True
    Sheets("Temp").Visible = True
    Cells.Select
    Selection.Copy
    Sheets("Old").Select
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1").Select
    Sheets("Temp").Select
    Range("A1").Select
    Sheets("Summary").Select
    Range("A1").Select

End Sub

Can anyone give me an idea of why this is going wrong? Maybe I have to select the newly opened spreadsheet? How would i do that?

Thanks in advance!
Jimmi
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I've stumbled across another code. This one looks like it's doing it - it opens all files and does 'something' but once it's finished and i open the files, i can see that nothing has happened.

VBA Code:
Sub RunOnAllFilesInFolder()

Dim wbOpen As Workbook
Dim MyDir As String
MyDir = "S:\Reports\Updates\"


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error Resume Next
strExtension = Dir(MyDir & "\*.xlsx")


While strExtension <> vbNullString
Set wbOpen = Workbooks.Open(MyDir & "\" & strExtension)

With wbOpen
.Worksheets(1).Cells(1, 1).Value = "Hello"
        Call Behindthescenes
        Call MainRun
.Close SaveChanges:=True
End With

strExtension = Dir
Wend

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Is anyone here able to help? I think I'm getting closer. Rather than calling the macro i've decided to try using the code.
This seems to freeze excel, but when i restart it, it's opens up the first file in the folder and copies from the wrong sheet - it doesn't paste it anywhere (I think this is because it doesn't unhide New, Old or Temp - the first lines of my code).

Any help would be appreciated.

VBA Code:
Private Sub RunOnAllFilesInFolder()

Dim wbOpen As Workbook
Dim MyDir As String
MyDir = "S:\Reports\Updates\"


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error Resume Next
strExtension = Dir(MyDir & "\*.xlsx")


While strExtension <> vbNullString
Set wbOpen = Workbooks.Open(MyDir & "\" & strExtension)

With wbOpen
      'here comes my code
    Sheets("New").Visible = True
    Sheets("Old").Visible = True
    Sheets("Temp").Visible = True
    Sheets("Temp").Select
    Cells.Select
    Selection.Copy
    Sheets("Old").Select
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1").Select
    Sheets("Temp").Select
    Range("A1").Select
    Sheets("Summary").Select
    Range("A1").Select
      'end of my code
.Close SaveChanges:=True
End With

strExtension = Dir
Wend

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,499
Messages
6,125,163
Members
449,210
Latest member
grifaz

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