You can achieve what you want using Excel formulas as well.
=IFERROR(SUM(INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))),"Not Found")
To understand this, let's create a basic formula and then break up the formula to understand how it works.
Let's say, your worksheet looks like

So what we want is =SUM(D7:D10)
? So let's break this up.
The formula can be broken up as =SUM("D" & "7" & ":D" & "10")
. Do not yet directly enter this in the cell. It will not work. Ok next thing is to make it dynamic so that we can get the 7
& 10
. Now 7
is the position of G
and 10
is the position of J
which we can get using MATCH
. For example
MATCH("G",C:C,0) and
MATCH("J",C:C,0)
So the formula can now be written as
=SUM("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))
"D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0)
is just a string at the moment. You have to use INDIRECT
to tell excel to consider it as a range. So the string now becomes
INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))
and then putting it inside the SUM
formula.
SUM(INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0)))
One final thing. The MATCH
can return an error if no match is found so we can tackle that using =IFERROR(FORMULA,"ERROR MESSAGE")
So we get our final formula
=IFERROR(SUM(INDIRECT("D" & MATCH("G",C:C,0) & ":D" & MATCH("J",C:C,0))),"Not Found")
one more question.. if i want to present the sum in another sheet in the same workbook, how should i change the formula?
Let's say the data is in Sheet1
so the formula in another sheet will look like
=SUM(Sheet1!D7:D10)
So now follow the above logic and you will get
=IFERROR(SUM(INDIRECT("Sheet1!D" & MATCH("G",Sheet1!C:C,0) & ":D" & MATCH("J",Sheet1!C:C,0))),"Not Found")