+ Reply to Thread
Results 1 to 7 of 7

Apply macro to all workbooks in folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Apply macro to all workbooks in folder

    Hi everyone,

    I have the following codes:

    Option Explicit
    
    Sub Procedure1()
    Dim N As Long
    Dim URNRow As Long
    
    For N = 2 To Sheets("Sheet1").Cells(Rows.Count, 13).End(xlUp).Row
        
        If WorksheetFunction.CountIf(Sheets("Sheet2").Range(Sheets("Sheet2").Columns(5), Sheets("Sheet2").Columns(10)), Sheets("Sheet1").Cells(N, 13)) > 0 Then
            URNRow = Sheets("Sheet2").Range(Sheets("Sheet2").Columns(5), Sheets("Sheet2").Columns(10)).Find(Sheets("Sheet1").Cells(N, 13), , xlValues, xlWhole).Row
            If Sheets("Sheet2").Cells(URNRow, 3).MergeArea(1) = Sheets("Sheet1").Cells(N, 31) Then
                Sheets("Sheet1").Range(Sheets("Sheet1").Cells(N, 13), Sheets("Sheet1").Cells(N, 20)).Interior.Color = Sheets("Sheet2").Cells(URNRow, 2).MergeArea(1).Interior.Color
            End If
        End If
    Next N
    End Sub
    Sub Procedure2()
    Dim N As Long
    Dim URNRow As Long
    
    For N = 2 To Sheets("Sheet1").Cells(Rows.Count, 13).End(xlUp).Row
        
        If WorksheetFunction.CountIf(Sheets("Sheet2").Range(Sheets("Sheet2").Columns(5), Sheets("Sheet2").Columns(10)), Sheets("Sheet1").Cells(N, 21)) > 0 Then
            URNRow = Sheets("Sheet2").Range(Sheets("Sheet2").Columns(5), Sheets("Sheet2").Columns(10)).Find(Sheets("Sheet1").Cells(N, 21), , xlValues, xlWhole).Row
            If Sheets("Sheet2").Cells(URNRow, 3).MergeArea(1) = Sheets("Sheet1").Cells(N, 31) Then
                Sheets("Sheet1").Range(Sheets("Sheet1").Cells(N, 21), Sheets("Sheet1").Cells(N, 28)).Interior.Color = Sheets("Sheet2").Cells(URNRow, 2).MergeArea(1).Interior.Color
            End If
        End If
    Next N
    
    End Sub
    I run them both via:

    Sub Colour()
    Procedure1
    Procedure2
    End Sub
    However, i was wondering if there was a way I could apply this to all of the workbooks in a particular folder? Simply to save having to do this to a large number of spreadsheets.

    There are 61 spreadsheets in a folder called "GR12 Schedules" and each file is called "GR12 Schedule" and then a number i.e Schedule 1, Schedule 2 etc up to 61

    Many thanks,

    Chris

  2. #2
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,494

    Re: Apply macro to all workbooks in folder

    I adapted this to open each file in a Folder, but if any of the workbooks are password protected, don't forget to use a "wildcard" when defining the file type to open.

    https://www.thespreadsheetguru.com/t...a-given-folder

    Hope it helps

    Ochimus
    Last edited by Ochimus; 12-11-2020 at 06:53 AM.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Apply macro to all workbooks in folder

    Change the folder path to suit.

    Sub Colour_All()
        Dim sPath As String, sFile As String, counter As Long
        
        sPath = "C:\GR12 Schedules\"
        
        sFile = Dir(sPath & "GR12 Schedules*.*")
        
        Application.ScreenUpdating = False
        Do While sFile <> ""
            With Workbooks.Open(sPath & sFile)
                Procedure1
                Procedure2
                .Close SaveChanges:=True
            End With
            counter = counter + 1
            sFile = Dir
        Loop
        Application.ScreenUpdating = True
        
        MsgBox counter & " files coloured.", , "Colour Files Complete"
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Apply macro to all workbooks in folder

    Hi AlphaFrog,

    Many thanks for your response, much appreciated

    I have amended your code to fit with the correct folder locations etc and I am getting "0 files coloured" messaged at the end.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Apply macro to all workbooks in folder

    Can you show your amended code?

  6. #6
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Apply macro to all workbooks in folder

    Sub Colour_All()
        Dim sPath As String, sFile As String, counter As Long
        
        sPath = "J:\XXXXXX\XXXXXXXX\CPA\Part 2\Schedules"
        
        sFile = Dir(sPath & "GR12 Schedule*.*")
        
        Application.ScreenUpdating = False
        Do While sFile <> ""
            With Workbooks.Open(sPath & sFile)
                Procedure1
                Procedure2
                .Close SaveChanges:=True
            End With
            counter = counter + 1
            sFile = Dir
        Loop
        Application.ScreenUpdating = True
        
        MsgBox counter & " files coloured.", , "Colour Files Complete"
        
    End Sub

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Apply macro to all workbooks in folder

    Missing the trailing \

    sPath = "J:\XXXXXX\XXXXXXXX\CPA\Part 2\Schedules\"

    Isn't your folder called "GR12 Schedules" ?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to make a recorded macro apply to various workbooks
    By NeedhelpwithExcel2 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-29-2017, 04:59 PM
  2. apply macros to a folder which has all the workbooks
    By gauti14 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-10-2017, 12:32 PM
  3. Apply a macro across a hundred workbooks, and then copying the results into a master Excel
    By icomefromchaos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-24-2014, 12:21 PM
  4. Apply Macro to all files in a specific folder.
    By AntiPivotTable in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2013, 12:48 PM
  5. Apply Macro to multiple Workbooks at Once
    By Prevostb in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-11-2012, 05:25 PM
  6. Apply Macro to a folder of files
    By Costasg in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2011, 07:56 AM
  7. Apply the same macro code to all excel files in a folder
    By Alano in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-09-2007, 09:26 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1