Macro to Consolidate all client files in one WB each month

bababooey707

New Member
Joined
Jan 11, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Each close month my analyst has to manually go to each individual clients sharepoint file and get a variance comment from them. I'm looking to create a macro to dump all the clients into one file and then I can dynamically look up to their sheet. There are a few tabs on the file, but I would just want the first tab called "Flash" from each client. I tried to use an Indirect formula, but realized it only works when each file is open and there are over 80+ clients.

The Sharepoint folder is organized like the below

Finance>General>NY>Client 1>Flash>2022>2022.12 - December> *Client File
Finance>General>LA>Client 2>Flash>2022>2022.12 - December> *Client File

*Client Files aren't always named in the same way, but there's only one file in the folder, so I would just want to grab any file under the current month.

And is it possible to name the tab after the Client name?

Appreciate any help at all and I will be continuing trying to think of a better way to do this! Let me know if anything was unclear.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:

VBA Code:
Sub ConsolidateData()

Dim wb As Workbook
Dim ws As Worksheet
Dim strFile As String
Dim strPath As String
Dim strClientName As String

'Change the file path to the folder where the client files are located
strPath = "C:\Finance\General\"

'Change the file extension if necessary
strFile = Dir(strPath & "*.xlsx")

'Activate the master sheet
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet

'Add column for client name
ws.Cells(1, 1) = "Client Name"

'Initialize the row variable
i = 2

'Loop through all the files in the folder
Do While strFile <> ""
    'Open the file
    Workbooks.Open strPath & strFile
    
    'Capture the client name
    strClientName = ActiveWorkbook.Name
    strClientName = Left(strClientName, InStr(strClientName, ".") - 1)
    
    'Select the flash sheet
    Sheets("Flash").Select

    'Copy the data
    Selection.Copy

    'Paste the data to master sheet
    ws.Cells(i, 1).PasteSpecial xlPasteValues
    ws.Cells(i, 1).PasteSpecial xlPasteFormats
    
    'add client name on the next column
    ws.Cells(i, 2) = strClientName

    'Close the file
    ActiveWorkbook.Close False

    'Move to the next row
    i = i + 1
    
    'Get the next file
    strFile = Dir

Loop

End Sub
 
Upvote 0
Try this:

VBA Code:
Sub ConsolidateData()

Dim wb As Workbook
Dim ws As Worksheet
Dim strFile As String
Dim strPath As String
Dim strClientName As String

'Change the file path to the folder where the client files are located
strPath = "C:\Finance\General\"

'Change the file extension if necessary
strFile = Dir(strPath & "*.xlsx")

'Activate the master sheet
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet

'Add column for client name
ws.Cells(1, 1) = "Client Name"

'Initialize the row variable
i = 2

'Loop through all the files in the folder
Do While strFile <> ""
    'Open the file
    Workbooks.Open strPath & strFile
   
    'Capture the client name
    strClientName = ActiveWorkbook.Name
    strClientName = Left(strClientName, InStr(strClientName, ".") - 1)
   
    'Select the flash sheet
    Sheets("Flash").Select

    'Copy the data
    Selection.Copy

    'Paste the data to master sheet
    ws.Cells(i, 1).PasteSpecial xlPasteValues
    ws.Cells(i, 1).PasteSpecial xlPasteFormats
   
    'add client name on the next column
    ws.Cells(i, 2) = strClientName

    'Close the file
    ActiveWorkbook.Close False

    'Move to the next row
    i = i + 1
   
    'Get the next file
    strFile = Dir

Loop

End Sub
Thanks for the help! But will this allow me to only pull "december 2022" if I want. Here's a detailed Example of how the folders are set up
 

Attachments

  • Capture.PNG
    Capture.PNG
    15.7 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,214,866
Messages
6,121,996
Members
449,060
Latest member
mtsheetz

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