+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27

Thread: Count data from multiple files

  1. #16
    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 fixed the macro to sum each representative's count based on the each day of the month. Here is the amended macro.
    'Thread:  http://www.excelforum.com/excel-programming/729324-count-data-from-multiple-files.html#post2307113
    'Poster:  Bob@Sun
    'Written: May 15, 2010
    'Updated: May 16, 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\Admin.ADMINS\My Documents\Excel Forum Folders\Bob@Sun"
        
        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) = Person.Offset(0, Month) + WorksheetFunction.CountIf(NameRng, Person)
                Next Person
               
                Wkb.Close False
              End If
              FileName = Dir()
              
            Loop
                
          Application.ScreenUpdating = True
          
    End Sub
    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!)

  2. #17
    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

    Hi Leith,

    I have posted another problem that I have faced. Probalby I did not have time to check that.



    Quote Originally Posted by Bob@Sun View Post
    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""))") +  Person.Offset(0, Month)
                Next Person
               
                Wkb.Close False
              End If
              FileName = Dir()
              
            Loop
                
          Application.ScreenUpdating = True
          
    End Sub

  3. #18
    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 don't understand your last post. It is looks to be a repost of the previous one. The macro in my last post fixes the monthly total problem. Did you not run it? Is there another problem? If so, please give me examples of the problems.
    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!)

  4. #19
    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

    Oh ok,

    My problem now is that I am trying to change the countif function with Evaluate("Sumproduct.....), but I am having problems with that.

    For Each Person In NameList
                   Person.Offset(0, Month) = Application.Evaluate("SUMPRODUCT(--(L1:L100=" & Person & "),--(M1:M100=""OK""))") +  Person.Offset(0, Month)
                Next Person
    The problem comes when declaring " & Person &" in the formula. If I write just the name like this ""Peter"" everything is ok, but when I chage it to Person I am getting error.

    What is wrong?
    Last edited by Bob@Sun; 05-16-2010 at 02:59 PM.

  5. #20
    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,

    "Person" is a cell in the range "NameList". NameList is the range of names in column "A" of "Sheet1" in workbook "Calculate Data.xls". Person represents the cell as a Range Object and not just the contents of the cell.
    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!)

  6. #21
    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 can I transfer Person to represent the contents of the cell?

  7. #22
    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,

    Try this ...
     
      & Person.Text &
    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!)

  8. #23
    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 get the "Type mismatch" error

  9. #24
    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 removed this...
    +  Person.Offset(0, Month)

    and I got true, but in the cell I still get the #NAME? error? while in the inmidiate window I am getting the names wright.

  10. #25
    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 Bobo@Sun,

    I don't know what the problem is because I don't understand the logic of the statement.
    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!)

  11. #26
    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

    If this is the whole code...

    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.Text & "),--(M1:M100=""OK""))") _
                               + Person.Offset(0, Month)
                Next Person
               
                Wkb.Close False
              End If
              FileName = Dir()
              
            Loop
                
          Application.ScreenUpdating = True
          
    End Sub
    If I run the code I am gettting "type mismatch" error.
    I guess I have to cange something in this part to make it work...

     For Each Person In NameList
                   Person.Offset(0, Month) = Application.Evaluate("SUMPRODUCT(--(L1:L100=" & Person.Text & "),--(M1:M100=""OK""))") _
                               + Person.Offset(0, Month)
                Next Person

  12. #27
    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 chage the code to this.....
    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"
          
          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)
          
    ReDim NamesFound(0) As String
    ReDim NamesFound(50) As String
    i = 0
    For Each Item In NameRng
                    
    Nameaddress = Item.Address
    
    
    If IsInArray(Item, NamesFound) = False Then
    NamesFound(i) = Item
                
        Wkb.Activate
                  
                    Count = Application.Evaluate("SUMPRODUCT(--(L1:L100=" & Nameaddress & "),--(M1:M100=""OK""))")
           With ThisWorkbook
           .Activate
                   Cells.Find(What:=NamesFound(i), After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Select
            
                    Selection.Offset(0, Month).Value = Selection.Offset(0, Month).Value + Count
            End With
    
    Else
    End If
    
    
     i = i + 1
    Next Item
         
    Wkb.Close False
    End If
    FileName = Dir()
    
            Loop
                
          Application.ScreenUpdating = True
          
    End Sub


    and this is doing the job

    Leith and JBeaucaire,

    Thanks for the help!

+ 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