+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Count data from multiple files

  1. #1
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Count data from multiple files

    Hi everybody,

    Here is the case for which I need your help:

    My files have their names like this "DataBase_20100301"
    The only thing that changes is the date extention.

    I have about 20 files for each month (each work day)
    Each file holds data for customer inquiries solved by constumer representative.
    Each row is one inquiry solve.

    I need to get statistics for number of inquiries solved by representative without consolidating the data in one excel sheet ( due to excel limitations)

    I have to build macro that will open each file and count inquiries of each name summing it up for the whole month and then get the results like this:


    Jan Feb etc…
    John 500 450
    Peter 450 540 etc….

    Hope that you can help me.

    Thanks!
    Last edited by Bob@Sun; 05-18-2010 at 06:57 AM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,225

    Re: Count data from multiple files

    I imagine a sample of one of the files to evaluate and a copy of your summary workbook/layout would get this started.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    Here are the test files!

    Thanks for the help.
    Attached Files Attached Files

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,225

    Re: Count data from multiple files

    What Excel limitations keep you from creating a consolidation file first, then gathering simple statistics from the resulting workbook?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    Excel has about 65,000 rows.

    Just a few files are equal to this amount, what about 12 months times 22 files each month ?

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,225

    Re: Count data from multiple files

    Well, consolidation files can take any form you wish. You're only interested in one column of data per sheet, yes? Column L? Actually, I would think a consolidation report would be a two column report...

    1st Col = Names from column L
    2nd Col = Jan/Feb/Mar, etc... based on the name of the source workbook

    When the bottom of the consolidation sheet is reached, continue in the next empty column over?

    The reason I ask is because a PivotTable can represent this report rather easily and can use multiple consolidation ranges. See the example sheet here:
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    Well I am looking for vba solution because the whole report and statistical calculations include much more then count number of inquiries.

    I was hoping that with the techniques of this simple report I would be able to build more complex report.

    That is why what I really need is vba solution to count number of inquiries by person. Once I know how to do this I will be able to do everything else.

    What you suggest is a good idea and I can use it in the future, but now I need the VBA solution.

    Thanks a lot for helping!

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,225

    Re: Count data from multiple files

    I have been talking about VBA, you notice the pivottable created in that sample workbook was created by VBA, yes?

    Anyway, I don't know a simple way to do what you've asked all in VBA, I would go the consolidation sheet route since it's what I know, then VBA the pivot table to get the count. But since you don't want to go that route and indicate there is far more to this than you've presented, then I'll defer to the next contributor.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Count data from multiple files

    Hello Bob@Sun,

    I have added the macro below to the attached workbook. You will need to change the directory where the files are stored. The macro will examine each file name in the directory for a pattern matching "Database_YYYYMMDD.xls". The workbook is opened and the count retrieved for each name. The master names list is taken from "Sheet1" column "A" of the "Calculate Data" workbook. The totals are then placed in month column for the representative. The month is derived from the file name. The process is simple and straightforward but if you have any questions, just ask.
    'Thread:  http://www.excelforum.com/excel-programming/729324-count-data-from-multiple-files.html#post2307113
    'Poster:  Bob@Sun
    'Written: May 15, 2010
    'Author:  Leith Ross
    
    Sub CountCalls()
    
      Dim CalcWks As Worksheet
      Dim FileName As String
      Dim FilePath As String
      Dim Person As Variant
      Dim Month As Integer
      Dim NameList As Range
      Dim NameRng As Range
      Dim RngEnd As Range
      
       'Change the folder to where your files are stored
        FilePath = "C:\Documents and Settings\Owner\My Documents"
    
        Set CalcWks = ThisWorkbook.Worksheets("Sheet1")
        
        Set NameRng = CalcWks.Range("A3")
        Set RngEnd = CalcWks.Cells(Rows.Count, NameRng.Column).End(xlUp)
        Set NameRng = IIf(RngEnd.Row > NameRng.Row, CalcWks.Range(NameRng, RngEnd), NameRng)
      
        Set NameList = NameRng
        
          Application.ScreenUpdating = False
          FileName = Dir(FilePath & "\")
          
            Do While FileName <> ""
              
              If FileName Like "*_########.xls" Then
                Set Wkb = Workbooks.Open(FilePath & "\" & FileName)
                Month = Val(Mid(Wkb.Name, 14, 2))
          
                Set NameRng = Wkb.Worksheets("Sheet1").Range("L2")
                Set RngEnd = NameRng.Parent.Cells(Rows.Count, NameRng.Column).End(xlUp)
                Set NameRng = IIf(RngEnd.Row > NameRng.Row, NameRng.Parent.Range(NameRng, RngEnd), NameRng)
          
                For Each Person In NameList
                  Person.Offset(0, Month) = WorksheetFunction.CountIf(NameRng, Person)
                Next Person
               
                Wkb.Close False
              End If
              FileName = Dir()
              
            Loop
                
          Application.ScreenUpdating = True
          
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    Leith,

    You are the best!

    I think this will work, but will test it with the real data once you go to work.

    Thanks so much! Be back with feedback in a few days.

    Bob

  11. #11
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    Leith,

    Testing the macro I have noticed that opening the first file everything is Ok. The macro is calculating everything right. With the second file it is not adding the count to the first one, but deleting it.

    What I need is to sum the count from each month.

    I was thinking to use arrays to store the values from each file opened and then sum the array and print the result to a cell.

    Do you think that this will be a good approach?

  12. #12
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,225

    Re: Count data from multiple files

    Maybe this tiny change:
    Person.Offset(0, Month) = Person.Offset(0, Month) + _
        WorksheetFunction.CountIf(NameRng, Person)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  13. #13
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    Yes, this works.

    Thanks, I will check if everything else is ok.

    Be back with a feedback.

    Cheers!

  14. #14
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    How I can add third criteria in the count if function?
    Column M holds Status: "OK" or "Not Completed"
    I have tried to change the code to this but is not working. Can I change the
    Count if function to Evaluate("Sumproduct"...) so I can add more criteria?


    Sub CountCalls()
    
      Dim CalcWks As Worksheet
      Dim FileName As String
      Dim FilePath As String
      Dim Person As Variant
      Dim Month As Integer
      Dim NameList As Range
      Dim NameRng As Range
      Dim RngEnd As Range
      Dim StatusRng As Range
      
       'Change the folder to where your files are stored
        FilePath = "D:\Documents\EXCEL VBA\TESTING"
        
        Set CalcWks = ThisWorkbook.Worksheets("Sheet1")
        
        Set NameRng = CalcWks.Range("A3")
        Set RngEnd = CalcWks.Cells(Rows.Count, NameRng.Column).End(xlUp)
        Set NameRng = IIf(RngEnd.Row > NameRng.Row, CalcWks.Range(NameRng, RngEnd), NameRng)
      
        Set NameList = NameRng
        
          Application.ScreenUpdating = False
          FileName = Dir(FilePath & "\")
          
            Do While FileName <> ""
              
              If FileName Like "*_########.xls" Then
                Set Wkb = Workbooks.Open(FilePath & "\" & FileName)
                Month = Val(Mid(Wkb.Name, 14, 2))
          
                Set NameRng = Wkb.Worksheets("Sheet1").Range("L2")
                Set RngEnd = NameRng.Parent.Cells(Rows.Count, NameRng.Column).End(xlUp)
                Set NameRng = IIf(RngEnd.Row > NameRng.Row, NameRng.Parent.Range(NameRng, RngEnd), NameRng)
                Set StatusRng = NameRng.Offset(0, 1)
    
                 For Each Person In NameList
                 Person.Offset(0, Month) = Person.Offset(0, Month) + _
                                WorksheetFunction.CountIf(NameRng, Person, StatusRng = "OK")
                Next Person
               
                Wkb.Close False
              End If
              FileName = Dir()
              
            Loop
                
          Application.ScreenUpdating = True
          
    End Sub

  15. #15
    Valued Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2003
    Posts
    428

    Re: Count data from multiple files

    I have tried to change the countif function with Evaluate.
    When I use ""Peter"" in the formula everything is ok, but when change it to " & Person & "
    I am getting an error.

    What is wrong?

    Sub CountCalls()
    
      Dim CalcWks As Worksheet
      Dim FileName As String
      Dim FilePath As String
      Dim Person As Variant
      Dim Month As Integer
      Dim NameList As Range
      Dim NameRng As Range
      Dim RngEnd As Range
      
       'Change the folder to where your files are stored
        FilePath = "D:\Documents\EXCEL VBA\TESTING"
        
        Set CalcWks = ThisWorkbook.Worksheets("Sheet1")
        
        Set NameRng = CalcWks.Range("A3")
        Set RngEnd = CalcWks.Cells(Rows.Count, NameRng.Column).End(xlUp)
        Set NameRng = IIf(RngEnd.Row > NameRng.Row, CalcWks.Range(NameRng, RngEnd), NameRng)
      
        Set NameList = NameRng
        
          Application.ScreenUpdating = False
          FileName = Dir(FilePath & "\")
          
            Do While FileName <> ""
              
              If FileName Like "*_########.xls" Then
                Set Wkb = Workbooks.Open(FilePath & "\" & FileName)
                Month = Val(Mid(Wkb.Name, 14, 2))
          
                Set NameRng = Wkb.Worksheets("Sheet1").Range("L2")
                Set RngEnd = NameRng.Parent.Cells(Rows.Count, NameRng.Column).End(xlUp)
                Set NameRng = IIf(RngEnd.Row > NameRng.Row, NameRng.Parent.Range(NameRng, RngEnd), NameRng)
          
                For Each Person In NameList
                   Person.Offset(0, Month) = Application.Evaluate("SUMPRODUCT(--(L1:L100=" & Person & "),--(M1:M100=""OK""))")
                Next Person
               
                Wkb.Close False
              End If
              FileName = Dir()
              
            Loop
                
          Application.ScreenUpdating = True
          
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0