VBA Excel large data manipulation taking forever

后端 未结 2 1715
眼角桃花
眼角桃花 2020-12-03 20:30

I have two excel files.

First excel file contains the Person Name and Total Days Present column Ex.

PersonName       TotalDays
xyz               
ab         


        
2条回答
  •  暖寄归人
    2020-12-03 21:00

    I would suggest a different approach.

    If I interpret the question correctly:

    • you want to get a count of days each person is "Present" or "Absent"
    • the first file (call it file1) contains one row per person (about 100 people)
    • the second file (call it file2) contains one row per person per day (100 people and 200 days = 20000 rows)
    • the desired output is a two extra columns in file 1, being a count of "Present" and a count of "Absent"

    The approach I would use is to use the COUNTIF (or if you hvae Excel 2007 or later COUNTIFS)

    Assume

    • file1 contains a Table on Sheet1 called StatusReport, columns A = Name, B = Present, C = Absent
    • one row for each unique name
    • file2 contains a Table on Sheet1 called StatusData, columns A = Name, B = Date, C = Status
    • one row for each name for each date

    Solution for Excel 2007 or 2010

    • file1 cell B2
      =COUNTIFS(file2.xlsx!StatusData[Name],[Name],file2.xlsx!StatusData[Status],StatusReport[[#Headers],[Present]])
    • file1 cell C2
      =COUNTIFS(file2.xlsx!StatusData[Name],[Name],file2.xlsx!StatusData[Status],StatusReport[[#Headers],[Absent]])

    Solution for Excel 2003

    • Add an extra column D to file2 StatusData table (call it Code)
      =Sheet1!$A2&"_"&Sheet1!$C2

    • file1 cell B2
      =COUNTIF([file2.xls]Sheet1!$D:$D,Sheet2!$A2&"_"&Sheet2!$B$1)

    • file1 cell C2
      =COUNTIF([file2.xls]Sheet1!$D:$D,Sheet2!$A2&"_"&Sheet2!$C$1)

    Note: while these formula give the same result the COUNTIFS + Table references version in 2010 if so much faster its not funny (my test on about 300,000 rows updates in a few seconds).

提交回复
热议问题