Link to home
Start Free TrialLog in
Avatar of Peter Weldon
Peter Weldon

asked on

Macro to sum data in a column based on values in 2 other columns, while counting the number of cells on which each sum is based

Hi,

I am looking for a macro that sums the values in a column based on a date and an ID, while counting the number of values that were summed for each date and ID.

I have attached some Sample Data, along with a (partial) screenshot (Sample Data Screenshot 1) with which to explain the problem. The screenshot is based on rows 1-33 from Sheet 3 of the Sample Data spreadsheet.
The data is ordered based on the initial date and time of each row (in columns A and B) and also based on an ID (in column E).

For a given date and a given ID, the values in column P, labelled “Dist”, need to be summed, while counting the number of values that were summed for each date and for each ID.

Using the screenshot as an example, rows 2-12 are on 29/11/2012 with the ID of IV4; rows 13-22 are on 30/11/2012 with the ID of IV4; and rows 23-33 are on 1/11/2012 with the ID of IV5. As long as the date in column A and the ID in column E are the same (i.e. before moving to a new date or ID), the values in column P need to be summed, and the number of values on which each sum is based need to be counted.

All other data in all other columns can be ignored/deleted.

For the output from the macro, for rows 2-12 this would be a sum of 55.58 and a count of 8; for rows 13-22 this would be a sum of 66.43 and a count of 8; and for rows 23-33 this would be a sum of 36.84 and a count of 11.

I anticipate that for each date and each ID the values in column P could be summed into the first row of each date and ID, and the subsequent rows could be deleted, with perhaps the count data going in column W (the first empty column). I have attached an additional screenshot (Sample Data Screenshot 2) of what I think the resultant dataset may look like.

Additionally, for the count data, there are only entries in column P when the value in column F is “T” (rather than “C”), and as such the counts should not take account of blank cells in column P. The dataset covers approximately 3 years of data.

Any help with making this macro is much appreciated and thanks in advance.
Sample-Data.xlsx
Sample-Data-Screenshot-1.jpg
Sample-Data-Screenshot-2.jpg
ASKER CERTIFIED SOLUTION
Avatar of Ejgil Hedegaard
Ejgil Hedegaard
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial