Results 1 to 6 of 6

Sum columns in multiple files

Threaded View

  1. #1
    Registered User
    Join Date
    04-14-2020
    Location
    Europe
    MS-Off Ver
    365
    Posts
    5

    Sum columns in multiple files

    Hey guys,

    So im trying to write and edit a macro to do the following.
    I give it a folder path > it goes through all the excel files in that folder and gives me the file name and 1) number of rows in it 2) sum of columns B and D.

    So all the files have similiar structure and headers. i.e for sum of columns it needs to start from B2,D2
    The problem im having is adding the column summing into the loop. Im not really sure how to.

    Please take a look at my code below, thanks everyone in advance!

    Sub CollectData()
    
        Dim fso As Object, xlFile As Object
        Dim sFolder$
        Dim r&, j&
        Dim X as Range 
        Dim Y as Range
    
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .InitialFileName = ThisWorkbook.Path
            If .Show Then sFolder = .SelectedItems(1) Else Exit Sub
        End With
        Set fso = CreateObject("Scripting.FileSystemObject")
        For Each xlFile In fso.GetFolder(sFolder).Files
            With Workbooks.Open(xlFile.Path)
                With .Sheets(1)
                    j = .Cells(.Rows.Count, 1).End(xlUp).Row
                    x = Application.WorksheetFunction.Sum(Range("B2") .End(xlDown))
    y = Application.WorksheetFunction.Sum(Range("D2") .End(xlDown))
                End With
                .Close False
            End With
            r = r + 1
            Cells(r, 1).Value = xlFile.Name
            Cells(r, 2).Value = j
            Cells(r, 3).Value = x
            Cells(r, 4).Value = y
    
        Next
    
    End Sub
    Last edited by Viacheslav; 07-13-2020 at 03:02 AM. Reason: Answered

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 10-04-2015, 08:21 AM
  2. [SOLVED] VBA to Copy the same Columns from multiple files in one folder
    By mcanant in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-16-2014, 03:15 AM
  3. Margin multiple columns in row from multiple excel files
    By dardanb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-09-2013, 03:36 PM
  4. Margin multiple columns in row from multiple excel files
    By dardanb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-12-2013, 03:51 PM
  5. Help importing multiple text files into individual columns
    By Jev1991 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-10-2013, 11:37 AM
  6. Create Multiple Files From Columns
    By tjs03e in forum Excel General
    Replies: 13
    Last Post: 10-06-2011, 07:09 PM
  7. Help exporting columns into multiple text files
    By vund0 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-02-2011, 05:09 PM

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