简体   繁体   中英

Dynamically sum row values based on column headers? - Excel

Need solutions for the queries posted in the below link,

http://www.mrexcel.com/forum/excel-questions/334740-dynamically-sum-row-values-based-column-headers.html

Having PersonWeek as separate column with sum of weeks should be displayed.

Quest: Dynamically sum row values based on column headers?

It is good form to re-post the question in its entirety on this site; nevertheless:

What you are looking for is relatively straight forward. There are a lot of ways to accomplish this but I suggest that you use combination of the 'Match' function and the the 'Offset' function. The Offset function creates a range based on a given start point, moving up/down / left/right as indicated, with a given height / width. For example:

=Offset(B2,1,2,3,4)

Indicates the range D3:G5. This is the range given by starting at cell B2, moving 1 row down and 2 columns to the right, and going for a total of 3 rows and 4 columns.

So the remaining point is to determine where to start and stop your offset.

First, the first field in your Offset function will be cell A1, as that is the top-left corner of your data table. To find how many rows to move down, you need to find what project you are referring to (I will assume that cell A6 is where you enter the project name that you care about, A7 is where you enter the first week you care about, and A8 is where you enter the last week you care about). To find how many rows to move down from A1, then, use Match:

=Match(A6, A2:A5,0)

To find how many columns to move to the right to find your first week, use Match again:

=Match(A7, B1:G1,0)

Assuming you only want to look at a single project, we know how high we want the range to be (1).

To find how wide we want the range to be, we need to know your ending week, less your starting week:

=(Match(A8,B1:G1,0)-Match(A7, B1:G1,0))

So the whole thing together will be :

=Offset(A1,Match(A6, A2:A5,0),Match(A7, B1:G1,0),1,Match(A8,B1:G1,0)-Match(A7, B1:G1,0))

Now the only thing left is to wrap the newly defined range in a 'sum' function, like so:

=Sum(Offset(A1,Match(A6, A2:A5,0),Match(A7, B1:G1,0),1,Match(A8,B1:G1,0)-Match(A7, B1:G1,0)))

This formula will create an error if someone enters a project name / weekname in a way that isn't found in your table - so in cells A6-A8 you may want to use data validation to only allow those names to be entered - let me know if you would like elaboration on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM