+ Reply to Thread
Results 1 to 14 of 14

How to modify the macro wich summarize all the files from the particular folder?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-01-2014
    Location
    RIX
    MS-Off Ver
    Excel 2003; 2007
    Posts
    114

    How to modify the macro wich summarize all the files from the particular folder?

    Hi!

    I've got a macro wich I use for consolidating everyday reports into one - it works great, but I'd like to modify it a bit.

    The criteria I'd like to add:

    1) I don't need all the content of each seperate file,-
    The content must be evaluated by column V5,
    consequently if value of column V5>0 then the row must be included in the summary;

    2) One more additional column in the summary is needed, which must be filled with the name of each particular file.

    The code I've been using till now:
    Sub Auto_Open()
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim i As Integer, ws As Worksheet, wt As Worksheet
    
    MyPath = ActiveWorkbook.Path
    
    MyName = ActiveWorkbook.Name: Set ws = Sheets("Summary")
    
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder(MyPath)
    
    Count = 0
    Sheets("Summary").Select
    
    For Each objFile In objFolder.Files
    If objFile.Name <> MyName Then
    Temp = objFile.Name
    If objFile.Name Like "*.xls*" Then
    MyLR = ws.Range("A65536").End(xlUp).Row + 1
    ChDir MyPath
    On Error GoTo ExitSub
    Workbooks.Open Filename:=Temp: Set wt = ActiveSheet
    
    wt.Range("A2", Cells(Range("A65536").End(xlUp).Row, 26)).Copy ws.Range("A" & MyLR)
    
    Windows(objFile.Name).Close False
    
    End If: End If
    
    Next objFile
    
    ExitSub: End Sub
    The files for consolidation attached:Summary-Macros.zip

    The expected resul would be: Expected-result.xls

    Thanks in advance!
    Regards

    Arty

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Hi Arty,

    Try this:

    Sub Auto_Open()
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim i As Integer, ws As Worksheet, wt As Worksheet, R As Range
    
    MyPath = ActiveWorkbook.Path
    
    MyName = ActiveWorkbook.Name: Set ws = Sheets("Summary")
    
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder(MyPath)
    
    Count = 0
    Sheets("Summary").Select
    
    For Each objFile In objFolder.Files
    If objFile.Name <> MyName Then
    Temp = objFile.Name
    If objFile.Name Like "*.xls*" Then
    MyLR = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
    ChDir MyPath
    On Error GoTo ExitSub
    Workbooks.Open Filename:=Temp: Set wt = ActiveSheet
    
    
            i = 1: Do Until wt.Cells(1, i) = "V5": i = i + 1: Loop
    Set R = wt.UsedRange.Offset(1): R.AutoFilter Field:=i, Criteria1:=">0"
    wt.UsedRange.SpecialCells(xlCellTypeVisible).Offset(1).Copy ws.Range("B" & MyLR)
            i = MyLR: Do Until ws.Cells(i + 1, 2) = "": i = i + 1: Loop
    ws.Range("A" & MyLR).Resize(i - MyLR + 1, 1).Value = wt.Parent.Name
            wt.AutoFilterMode = False
    
    Windows(objFile.Name).Close False
    
    End If: End If
    
    Next objFile
    
    ExitSub: End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    05-01-2014
    Location
    RIX
    MS-Off Ver
    Excel 2003; 2007
    Posts
    114

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Hi, xladept! Thanks for yor help!

    I was trying to consolidate 5 files, 22000 rows each, consequently 590 rows should be shown as a result in the summary file, but it's just 90 rows,

    furthermore the values aren't filtred by the determined criteria ">0"

    I did change the column name in the code.

    P.S.
    The number of the column: 5th (the same as in attached files)

    Any thoughts?

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Make the red code into whatever the new column name is:

    Sub Auto_Open()
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim i As Integer, ws As Worksheet, wt As Worksheet, R As Range
    
    MyPath = ActiveWorkbook.Path
    
    MyName = ActiveWorkbook.Name: Set ws = Sheets("Summary")
    
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder(MyPath)
    
    Count = 0
    Sheets("Summary").Select
    
    For Each objFile In objFolder.Files
    If objFile.Name <> MyName Then
    Temp = objFile.Name
    If objFile.Name Like "*.xls*" Then
    MyLR = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
    ChDir MyPath
    On Error GoTo ExitSub
    Workbooks.Open Filename:=Temp: Set wt = ActiveSheet
    
    
            i = 1: Do Until wt.Cells(1, i) = "V5": i = i + 1: Loop
    Set R = wt.UsedRange.Offset(1): R.AutoFilter Field:=i, Criteria1:=">0"
    wt.UsedRange.SpecialCells(xlCellTypeVisible).Offset(1).Copy ws.Range("B" & MyLR)
            i = MyLR: Do Until ws.Cells(i + 1, 2) = "": i = i + 1: Loop
    ws.Range("A" & MyLR).Resize(i - MyLR + 1, 1).Value = wt.Parent.Name
            wt.AutoFilterMode = False
    
    Windows(objFile.Name).Close False
    
    End If: End If
    
    Next objFile
    
    ExitSub: End Sub

  5. #5
    Forum Contributor
    Join Date
    05-01-2014
    Location
    RIX
    MS-Off Ver
    Excel 2003; 2007
    Posts
    114

    Re: How to modify the macro wich summarize all the files from the particular folder?

    I wrote before, that I changed the column name in the code.

    Not working - could the size of the large files I try to consolidate be the reason of the issue?

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to modify the macro wich summarize all the files from the particular folder?

    The size shouldn't matter - can you post new samples and/or your new code??

  7. #7
    Forum Contributor
    Join Date
    05-01-2014
    Location
    RIX
    MS-Off Ver
    Excel 2003; 2007
    Posts
    114

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Here are the other bigger files: 14-02-2015.zip15-02-2015.zip16-02-2015.zip

  8. #8
    Forum Contributor
    Join Date
    05-01-2014
    Location
    RIX
    MS-Off Ver
    Excel 2003; 2007
    Posts
    114

    Re: How to modify the macro wich summarize all the files from the particular folder?

    This is the code I used:

    Sub Auto_Open()
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim i As Integer, ws As Worksheet, wt As Worksheet, R As Range
    
    MyPath = ActiveWorkbook.Path
    
    MyName = ActiveWorkbook.Name: Set ws = Sheets("Summary")
    
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder(MyPath)
    
    Count = 0
    Sheets("Summary").Select
    
    For Each objFile In objFolder.Files
    If objFile.Name <> MyName Then
    Temp = objFile.Name
    If objFile.Name Like "*.xls*" Then
    MyLR = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
    ChDir MyPath
    On Error GoTo ExitSub
    Workbooks.Open Filename:=Temp: Set wt = ActiveSheet
    
    
            i = 1: Do Until wt.Cells(1, i) = "RESERVE": i = i + 1: Loop
    Set R = wt.UsedRange.Offset(1): R.AutoFilter Field:=i, Criteria1:=">0"
    wt.UsedRange.SpecialCells(xlCellTypeVisible).Offset(1).Copy ws.Range("B" & MyLR)
            i = MyLR: Do Until ws.Cells(i + 1, 2) = "": i = i + 1: Loop
    ws.Range("A" & MyLR).Resize(i - MyLR + 1, 1).Value = wt.Parent.Name
            wt.AutoFilterMode = False
    
    Windows(objFile.Name).Close False
    
    End If: End If
    
    Next objFile
    
    ExitSub: End Sub

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Hi Arty,

    Try this:

    Sub Auto_Open()
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim i As Integer, ws As Worksheet, wt As Worksheet, R As Range
    
    MyPath = ActiveWorkbook.Path
    
    MyName = ActiveWorkbook.Name: Set ws = Sheets("Summary")
    
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder(MyPath)
    
    Count = 0
    Sheets("Summary").Select
    
    For Each objFile In objFolder.Files
    If objFile.Name <> MyName Then
    Temp = objFile.Name
    If objFile.Name Like "*.xls*" Then
    MyLR = ws.Range("B" & Rows.Count).End(xlUp).Row
    ChDir MyPath
    On Error GoTo ExitSub
    Application.DisplayAlerts = False
    Workbooks.Open Filename:=Temp: Set wt = ActiveSheet
    
    
            i = 1: Do Until Trim(wt.Cells(1, i)) = "RESERVE": i = i + 1: Loop
    Set R = wt.UsedRange: R.AutoFilter Field:=i, Criteria1:=">0"
    wt.UsedRange.SpecialCells(xlCellTypeVisible).Copy
    ws.Range("B" & MyLR).PasteSpecial xlPasteAll
            i = MyLR: Do Until ws.Cells(i + 1, 2) = "": i = i + 1: Loop
    ws.Range("A" & MyLR).Resize(i - MyLR + 1, 1).Value = wt.Parent.Name
          If MyLR > 2 Then ws.Rows(MyLR).Delete Shift:=xlUp
            wt.AutoFilterMode = False
    
    Windows(objFile.Name).Close False
    
    End If: End If
    
    Next objFile
    
    ExitSub: Application.DisplayAlerts = True: End Sub

  10. #10
    Forum Contributor
    Join Date
    05-01-2014
    Location
    RIX
    MS-Off Ver
    Excel 2003; 2007
    Posts
    114

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Hi xladept!

    Now it's better but anyway I'm facing an issue:

    I tried to consolidate 4 files, as a result only 589 rows were included in the summary file. In fact 591 rows must be there.

    I've sorted the data of every seperate file by column REZERVAS in descending sequence,

    it's obvious that every last row of column REZERVAS at value > 0

    isn't included in the summary file.

    It can be checked by the column A:
    The codes which aren't included:
    CODE145
    CODE305
    CODE468

    I've attached the files and the summary with the code I've used.


    Attachment 377820Attachment 377821Attachment 377823Attachment 377824Attachment 377825

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to modify the macro wich summarize all the files from the particular folder?

    I found my mistake

    Sub Auto_Open()
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim i As Integer, ws As Worksheet, wt As Worksheet, R As Range
    
    MyPath = ActiveWorkbook.Path
    
    MyName = ActiveWorkbook.Name: Set ws = Sheets("Summary")
    
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder(MyPath)
    
    Count = 0
    Sheets("Summary").Select
    
    For Each objFile In objFolder.Files
    If objFile.Name <> MyName Then
    Temp = objFile.Name
    If objFile.Name Like "*.xls*" Then
    MyLR = ws.Range("B" & Rows.Count).End(xlUp).Row
    ChDir MyPath
    On Error GoTo ExitSub
    Application.DisplayAlerts = False
    Workbooks.Open Filename:=Temp: Set wt = ActiveSheet
    
    
            i = 1: Do Until Trim(wt.Cells(1, i)) = "RESERVE": i = i + 1: Loop
    Set R = wt.UsedRange: R.AutoFilter Field:=i, Criteria1:=">0"
                If MyLR > 2 Then MyLR = MyLR + 1
    wt.UsedRange.SpecialCells(xlCellTypeVisible).Copy ws.Range("B" & MyLR)
            i = MyLR: Do Until ws.Cells(i + 1, 2) = "": i = i + 1: Loop
    ws.Range("A" & MyLR).Resize(i - MyLR + 1, 1).Value = wt.Parent.Name
                If MyLR > 2 Then ws.Rows(MyLR).Delete Shift:=xlUp
            wt.AutoFilterMode = False
    
    Windows(objFile.Name).Close False
    
    End If: End If
    
    Next objFile
    
    ExitSub: ws.Cells(1, 1) = "File Name": Application.DisplayAlerts = True: End Sub
    Last edited by xladept; 02-17-2015 at 03:35 PM.

  12. #12
    Forum Contributor
    Join Date
    05-01-2014
    Location
    RIX
    MS-Off Ver
    Excel 2003; 2007
    Posts
    114

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Finally we've got it!

    It's brilliant!

    It'll save LOTS of time and nerves!

    Thanks a lot xladept!

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to modify the macro wich summarize all the files from the particular folder?

    Hi Arty,

    You say you're missing two rows but three codes? I couldn't access your attachments so I can't look yet - I'll go over the files from yesterday and examine my code??

    BTW - You realize that I'm only writing the first header don't you?
    Last edited by xladept; 02-17-2015 at 03:07 PM.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to modify the macro wich summarize all the files from the particular folder?

    You're welcome and thanks for the rep!

    (It was a stupid mistake - aren't they all)

+ 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. Macro to create a folder and then save all files in that folder
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2013, 01:49 PM
  2. Replies: 1
    Last Post: 03-12-2013, 04:45 AM
  3. Macro to Summarize daily files by copying blocks from daily files.
    By menoninblack in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-26-2012, 07:26 AM
  4. Modify Macro To Save In To Another Folder
    By JimmiOO in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-12-2010, 01:38 PM
  5. modify ALL files in a folder
    By TedH in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-13-2009, 04:53 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