+ Reply to Thread
Results 1 to 6 of 6

VBA code to sum across workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    65

    Question VBA code to sum across workbooks

    Hi all,

    I have a folder full of csv's, some of which are matrices of passenger boardings between rail stations for different times of day and ticket types.
    The csv's are all set up identically, with a header row & column (5 rail stations), and 5x5 matrix of data.

    In my Master workbook, I'm developing a VBA script that consolidates all the individual matrices into one aggregate matrix.
    For example, if the values in cell D8 of wb1, wb2 & wb3 are 5, 8, & 12, I'd want the value 25 in D8 of the master.

    I've attached the Master with VBA script so far.
    I think i need some way of creating an array variable that stores the running sum of values as the script moves through the csv's.
    I want to keep this dynamic so that the folder of csv's can be updated.

    FYI, in reality, there are about 40 csv's in the folder, and the matrices are more like 200x200, not 5x5, so an approach which is appropriate for more data would be great! Also open to Query solutions, though I'm pretty beginner with Queries.

    Any help would be greatly appreciated!
    Attached Files Attached Files

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,448

    Re: VBA code to sum across workbooks

    Hi aurelien,

    Your problem looks like it needs Power Query where all files are in the same folder. Looking at the VBA it seems you do what Power Query does already but easier.
    Here is only one of the many YouTube videos that explain what I mean.
    https://www.youtube.com/watch?v=Nbhd0B5ldJE
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    65
    Quote Originally Posted by MarvinP View Post
    Hi aurelien,

    Your problem looks like it needs Power Query where all files are in the same folder. Looking at the VBA it seems you do what Power Query does already but easier.
    Here is only one of the many YouTube videos that explain what I mean.
    https://www.youtube.com/watch?v=Nbhd0B5ldJE
    Thanks Marvin, that's useful info and I've learned something new about Power Query! Unfortunately this doesn't look like I can easily automate the process so it picks up the correct files in the folder and works on other folders. I'm hoping to minimise the user input incase this spreadsheet gets passed onto someone less Excel-savvy. A directory input cell and a Run button linked to a VBA script may be better in my circumstance. Thank you though!

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,448

    Re: VBA code to sum across workbooks

    Hey aurelien,

    Power Query is 10 times easier than VBA. If you have the PQ script set the other user would only need to click on the Data and Refresh to do all the work.
    You can write the PQ scripts to only include .csv files in your folder. Or you can filter it to only include files that include "2024" for example.
    Here is a better Video to show what I mean.
    https://www.youtube.com/watch?v=K-nal4mlVq8

  5. #5
    Valued Forum Contributor
    Join Date
    05-02-2013
    Location
    Poland
    MS-Off Ver
    Excel 2013
    Posts
    366

    Re: VBA code to sum across workbooks

    Number_of_csvs = Control_Sheet.Range("A10").End(xlDown).Row

    is an error because A10:A... are empty

    Rather Number_of_csvs = j (number of CSV files = j)
    -----------
    Try it
    Sub SUM_WBs()
    Dim csv As Variant, i As Long, j As Long, k As Long, wb As Workbook
    Dim result(), data()
        Application.Calculation = xlManual
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
        ReDim result(1 To 5, 1 To 5)    ' change to (1 to 200, 1 to 200) for 200x200 matrix
    
        Set Control_Sheet = ThisWorkbook.Worksheets("Control")
        ImportFilePath = Control_Sheet.Cells(5, 2)              '   folder with csv's becomes ImportFilePath variable
        
        csv = Dir(ImportFilePath & "*.csv")
    '    k = 0
        Do While csv <> ""
            If InStr(csv, "pax") > 0 Then                       'if the csv contains 'pax' in the filename
    '            k = k + 1
                Set wb = Workbooks.Open(ImportFilePath & csv)       'wb opens the csv
                data = wb.Worksheets(1).Range("B2").Resize(5, 5).Value       '   change to (200, 200) for 200x200 matrix
                wb.Close SaveChanges:=False
                For i = 1 To UBound(result, 1)
                    For j = 1 To UBound(result, 2)
                        result(i, j) = result(i, j) + data(i, j)    '   SUM
                    Next j
                Next i
        '        Control_Sheet.Cells(10 + k, 2).Value = csv      '  write the csv filename
            End If
            csv = Dir()
        Loop
        ThisWorkbook.Worksheets("All").Range("B2").Resize(UBound(result, 1), UBound(result, 2)).Value = result      '   change to (200, 200) for 200x200 matrix
        Application.ScreenUpdating = True
    End Sub
    In your free time, learn PQ
    Last edited by hungt; 04-25-2025 at 03:46 AM.

  6. #6
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,301

    Re: VBA code to sum across workbooks

    Quote Originally Posted by aurelien_21 View Post
    ... I have a folder full of csv's, some of which are matrices ...
    The csv's are all set up identically, with a header row & column ...
    matrix of data ...
    To make sure about the source data:
    1. Are the data in your csv files separated by tabs (as it would appear from the "example file" - "Example of csv_files" sheet) or perhaps by some other separator-delimiter ?
    2. Is the phrase "pax" in the file name the only distinguishing feature of a csv file with a data matrix ?

+ 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. Editing VBA code in other workbooks using VBA code
    By AB91 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-13-2017, 08:47 PM
  2. [SOLVED] Security around VBA code & workbooks
    By kenadams378 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-29-2013, 11:44 AM
  3. [SOLVED] Same Code for Multiple Workbooks?
    By Jester0001 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2012, 01:06 PM
  4. Common Code between workbooks
    By Eric Griffiths in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-27-2011, 02:36 PM
  5. Referring to other workbooks via VBA code
    By alexthecheese in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-06-2009, 01:59 PM
  6. Shared VBA code between workbooks
    By Vindaloo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2006, 10:16 AM
  7. Code for all workbooks
    By The unFriendly XTech IT Dept in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2006, 11:00 AM

Tags for this Thread

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