# how can I sum a variable number of columns within one row

#### msampson

##### Board Regular
I have a large data set with each different row being a different entity and many columns of calculations and inputs for that entity. I need to sum a variable number of columns of data depending on the age in column A for that particular entity.
In other words I have each possible age in columns AG:CQ and I want to sum from AG to whatever column matches the actual age in A. I assume some version of index, match and possibly sumif is what I need to combine.

A. AG. AH. AI. AJ. AK. AL. AM. AN. AO. AP. AQ. AR. AS
1. age. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32
2 25
3. 31

So for the patient in row 2 I would want to sum from AG2:AL2 but for the patient in row 3 I would want to sum AG3:AR3

Maureen
Excel for Mac v 16.42

### Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

#### Fluff

##### MrExcel MVP, Moderator
I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Excel Formula:
``=SUM(AG2:INDEX(AG2:CQ2,A2-19))``

#### Peter_SSs

##### MrExcel MVP, Moderator
Another option that would require no maintenance if the values in AG1:CQ1 were changed (eg starting at 18 instead of 20)

20 12 29.xlsm
ABCAGAHAIAJAKALAMANAOAPAQARAS
120212223242526272829303132
225337465928958544
331561715319487371
SUM variable
Cell Formulas
RangeFormula
B2:B3B2=SUMIF(AG\$1:CQ\$1,"<="&A2,AG2:CQ2)

Last edited:

#### msampson

##### Board Regular
Thanks! I got the first one to work, but I like the simplicity of the second one.

#### Fluff

##### MrExcel MVP, Moderator
Glad we could help & thanks for the feedback.

Replies
4
Views
72
Replies
1
Views
268
Replies
1
Views
18
Replies
5
Views
371
Replies
4
Views
144

1,129,795
Messages
5,638,367
Members
417,023
Latest member
Zimbo38

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back