Here's my answer:
=SUM(INDIRECT(ADDRESS(ROW(),8) & ":" & ADDRESS(ROW(),MATCH($D$1,$H$1:$Z$1)+7)))
 
Here's my logic:
You want to use the =SUM() function to calculate the total
for the current row from column H1 through the column described in D1.
 
For Jun-12, the SUM() function would look like:
=SUM(H2:M2)
or
=SUM($H$2:$M$2)
 
To get this, we need to "build" the string: $H$2:$M$2
 
For "$H$2", you can use the ADDRESS() function.
The syntax is:  ADDRESS(row,column)
the column number for "H" is 8,
but you don't want to use row (2) all of the time, you want the CURRENT row.
for that, you can use: ROW()
so, for "$H$2", you use:
ADDRESS(ROW(),8)
 
To get "$M$2", you need to find the column for the matching date.
For that, use MATCH()
=MATCH($D$1,$H$1:$Z$1) 
would result in (6)
But, this is 6 columns from the Jan-12 column (H), which is in column 8
So, to get the absolute (actual) column number, you need to add 7.
Which gives:
=MATCH($D$1,$H$1:$Z$1)+7
which, for "Jun-12" is (13).
 
using this in the ADDRESS(), function:
the syntax is: =ADDRESS(row,column)
you get: 
=ADDRESS(ROW(),MATCH($D$1,$H$1:$Z$1)+7)
-------------------------------------------
which, for row 2 results in: "$M$2"
 
making this into a string that represents a "range" needs the ":" between, so 
you end up with:
ADDRESS(ROW(),8) & ":" & ADDRESS(ROW(),MATCH($D$1,$H$1:$Z$1)+7)
 
The problem is, this is a STRING.
the SUM() function wants a RANGE.
 
There is a function that "interprets" a string as a Range.
The function is INDIRECT()
 
So, now you have:
=INDIRECT(ADDRESS(ROW(),8) & ":" & ADDRESS(ROW(),MATCH($D$1,$H$1:$Z$1)+7))
 
Last, put this into a SUM() function, and you get:
 
=SUM(INDIRECT(ADDRESS(ROW(),8) & ":" & ADDRESS(ROW(),MATCH($D$1,$H$1:$Z$1)+7)))
 
Paul
-----------------------------------------
“Do all the good you can,
By all the means you can,
In all the ways you can,
In all the places you can,
At all the times you can,
To all the people you can,
As long as ever you can.” - John Wesley
-----------------------------------------


>________________________________
>From: Excel_Lover <idforex...@gmail.com>
>To: excel-macros@googlegroups.com 
>Sent: Friday, July 19, 2013 7:35 AM
>Subject: $$Excel-Macros$$ To SUM Dynamic Columns
>
>
>
>Dear All, 
>
>
>Attached is a sample data in which I have some month wise item total, I need a 
>formula which can calculate SUM of an item from the Month Jan-12 to till the 
>month which  we are selecting in the column D1.
>
>
>Appreciate if somebody can help me with formula.
>
>
>
>Best Regards 
>Excel_Lover-- 
>Are you =EXP(E:RT) or =NOT(EXP(E:RT)) in Excel? And do you wanna be? It’s 
>=TIME(2,DO:IT,N:OW) ! Join official Facebook page of this forum @ 
>https://www.facebook.com/discussexcel
> 
>FORUM RULES
> 
>1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
>Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
>quick attention or may not be answered.
>2) Don't post a question in the thread of another member.
>3) Don't post questions regarding breaking or bypassing any security measure.
>4) Acknowledge the responses you receive, good or bad.
>5) Jobs posting is not allowed.
>6) Sharing copyrighted material and their links is not allowed.
> 
>NOTE : Don't ever post confidential data in a workbook. Forum owners and 
>members are not responsible for any loss.
>--- 
>You received this message because you are subscribed to the Google Groups "MS 
>EXCEL AND VBA MACROS" group.
>To unsubscribe from this group and stop receiving emails from it, send an 
>email to excel-macros+unsubscr...@googlegroups.com.
>To post to this group, send email to excel-macros@googlegroups.com.
>Visit this group at http://groups.google.com/group/excel-macros.
>For more options, visit https://groups.google.com/groups/opt_out.
> 
> 
>
>
>
-------------------------------------------

-- 
Are you =EXP(E:RT) or =NOT(EXP(E:RT)) in Excel? And do you wanna be? It’s 
=TIME(2,DO:IT,N:OW) ! Join official Facebook page of this forum @ 
https://www.facebook.com/discussexcel

FORUM RULES

1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
quick attention or may not be answered.
2) Don't post a question in the thread of another member.
3) Don't post questions regarding breaking or bypassing any security measure.
4) Acknowledge the responses you receive, good or bad.
5) Jobs posting is not allowed.
6) Sharing copyrighted material and their links is not allowed.

NOTE  : Don't ever post confidential data in a workbook. Forum owners and 
members are not responsible for any loss.
--- 
You received this message because you are subscribed to the Google Groups "MS 
EXCEL AND VBA MACROS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to excel-macros+unsubscr...@googlegroups.com.
To post to this group, send email to excel-macros@googlegroups.com.
Visit this group at http://groups.google.com/group/excel-macros.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to