Combining UDF with IF function

AC2411

New Member
Joined
Aug 10, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
I need to combine a UDF (below) with a set of IF functions.

Function MaxN(n&, r As Range)
Dim i&, j&, m#, t#, v
v = r.Cells.Value2
For i = 1 To UBound(v)
If UBound(v) - i + 1 >= n Then
t = 0
For j = i To i + n - 1
t = t + v(j, 1)
Next
If t > m Then m = t
Else
Exit For
End If
Next
MaxN = m
End Function

I'm trying to use this UDF (which finds the max value of 5 consecutive cells in a range) in combination with IF functions.

Eg: IF(A1=D1) and IF(B1=E1) then apply the UDf, otherwise leave blank (or = 0)

Appreciate the help!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
=IF(AND(A1=D1,B1=E1),MAXN(1,A1:A5),0)
I've tried this formula, however it always returns zero (even if there are values there).

I'm trying to use the UDF on a large range of data IF it occurs on month (0-12) and year (2000-2017).

Basically my cells look like below, and I'm using the formula {=IF(AND($B$2:$B$427613=H2,$C$2:$C$427613=$F$3),maxn(5,$D$2:$D$427613),0)}

1628652971372.png
 

Attachments

  • 1628652901602.png
    1628652901602.png
    58.2 KB · Views: 12
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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