Count but only on last 4 columns of dynamic range

Buzzcut

New Member
Joined
Dec 16, 2014
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Morning,

I've got most of my formula working, I just can't see limit the function to the last 4 columns of the range.

I'm adding 2 new columns per day and only want the sum of the last 4 on a rolling basis. The logic is that as time goes on there should be fewer counts for PERM over the last 2 days and the counts should all go to 0.

1620721224800.png


The "Count All PERM" can easily be done with =SUMIF($L$2:$S$2,"*PERM*",$L3:$S3) but the result in Col C is what I'm after.
I've been trying COLUMNS and INDEX but they all need a specific range, and my range keeps extending every day.

I can find the number of the last column (11) by using =LOOKUP(2,1/ (1:1<>""),ROW(A:A)) but I'm not sure how to pass this to the range.

Any ideas, folks?

Thanks...Buzz
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Use this Both UDF.
1. first for Count ALL Perm
2. For Count 4 Last Column

VBA Code:
Public Function CntAll(Rng As Range) As Long
Dim i As Long, Lc As Long
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
CntAll = Application.WorksheetFunction.SumIf(Range(Cells(1, 4), Cells(1, Lc)), "Perm*", Range(Cells(Rng.Row, 4), Cells(Rng.Row, Lc)))
End Function

VBA Code:
Public Function CntL4CL(Rng As Range) As Long
Dim i As Long, Lc As Long
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
CntL4CL = Application.WorksheetFunction.SumIf(Range(Cells(1, Lc - 3), Cells(1, Lc)), "Perm*", Range(Cells(Rng.Row, Lc - 3), Cells(Rng.Row, Lc)))
End Function
After Input them in VBA. Come Back to excel window and write Function name and Select Range that you want Count
Example:
Excel Formula:
=CntAll(D2:O2)
Excel Formula:
=CntL4CL(D2:O2)
 
Upvote 0
Thanks, but I should have mentioned this needs to be done with Formulas rather than VBA.

I've got the SUM working for the last 4 columns but not for only when the heading is PERM

=SUM(OFFSET(D2,0,COUNTA($D$1:$XFD$1)-1,1,-4))
 
Upvote 0
I suggest that you update your Account details (or 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’)
 
Upvote 0
@Buzzcut

Maybe this in C2

Excel Formula:
=SUMPRODUCT(OFFSET(D2,0,COUNTA($D$1:$XFD$1)-1,1,-4)*(LEFT(OFFSET(D$1,0,COUNTA($D$1:$XFD$1)-1,1,-4),4)="PERM"))

Hope that helps
 
Upvote 0
Solution
@Snakehips - Amazing, that works great.
But, unfortunately, still no record of what version(s) you are using. :(

For example, if Excel 365 and the LET function, it could be done like this without the multiple uses of the volatile function OFFSET.
Adjust ranges if needed of course.

21 05 11.xlsm
CDEFGHIJKLM
1ALS 8PERM 8ALS 9PERM 9ALS 10PERM 10ALS 11PERM 11
20111111
3211111111
411111
Sum in Last 4
Cell Formulas
RangeFormula
C2:C4C2=LET(seq,SEQUENCE(,4,COUNTA(D$1:AY$1),-1),SUM(FILTER(INDEX(D2:AY2,seq),LEFT(INDEX(D$1:AY$1,seq),1)="P")))
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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