Sumif using a dynamic range in the formula

kmr47

New Member
Joined
Sep 1, 2014
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi
I'd appreciate help on the correct syntax for a SUMIF formula using dynamic ranges from variables

The following basic formula works great, but I need to use dynamic ranges and have failed in my attempts
=Sumif(A:A,A5,K:K)
this sums the values in Column K where the value in Column A matches A5

I want the ranges in A and K to start at say row 28 and end at a row number that is read from a variable called lastrow. As an example assume lastrow value is 50. So the eventual formula to be executed would look like
=Sumif(A28:A50,A5,K28:K50) How do I replace the A50 and K50 with A+lastrow and K+lastrow

Thanks in advance for help
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Excel 2010
ABCDE
LASTROW

<tbody>
[TD="align: center"]1[/TD]
[TD="align: right"]54[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: right"]60[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]54[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet1

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH]A1[/TH]
[TD="align: left"]=SUMIF(INDIRECT("A28:A" & E1),A5,INDIRECT("K28:K" &E1))[/TD]
[/TR]
[TR]
[TH]A2[/TH]
[TD="align: left"]=SUMIF(OFFSET(A28,0,0,E1+1-ROW(A28)),A5,OFFSET(K28,0,0,E1+1-ROW(A28)))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

Either of the above will work if you want formulae.. (where E1 is the variable for LastRow)




The code below shows within VBA

Code:
Sub sumiftest()


    lastrow = 50


    Sheets("sheet1").Range("A1").Formula = "=SUMIF(A28:A" & lastrow & ",A5,K28:K" & lastrow & ")"


End Sub
 
Last edited:
Upvote 0
Thanks for the rapid response.Your answer is written in a very clear style and it all works well. Much appreciated:)
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top