VBA alternative for MAXIFS (to use with non Office365 versions)

kapvg

New Member
Joined
Jun 30, 2018
Messages
25
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi,

Although I frequent Mr. Excel to figure out resolutions for my VBA issues, this is my first post here coz after racking my head on this repeatedly I have not gotten anywhere :(

The data scenario:

I have over 15,000 rows of data in a format similar to the sample table below:
[TABLE="width: 355"]
<tbody>[TR]
[TD="align: center"]S.No[/TD]
[TD="align: center"]Account[/TD]
[TD="align: center"]Month counter[/TD]
[TD="align: center"]Max S.No for a given Account and Month[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]3[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]WXYZ[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]5[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]WXYZ[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]5[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]6[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]10[/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]10[/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]10[/TD]
[/TR]
[TR]
[TD="align: center"]10[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]10[/TD]
[/TR]
[TR]
[TD="align: center"]11[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]14[/TD]
[/TR]
[TR]
[TD="align: center"]12[/TD]
[TD="align: center"]WXYZ[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]15[/TD]
[/TR]
[TR]
[TD="align: center"]13[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]14[/TD]
[/TR]
[TR]
[TD="align: center"]14[/TD]
[TD="align: center"]ABCD[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]14[/TD]
[/TR]
[TR]
[TD="align: center"]15[/TD]
[TD="align: center"]WXYZ[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]15[/TD]
[/TR]
</tbody>[/TABLE]

I need to get the max S.No for each account for each month in the 4th column (column D).

The problem:
I am able to use the MAXIFS formula =MAXIFS($A$2:$A$16,$B$2:$B$16,$B2,$C$2:$C$16,$C2) to get the results that I want in column D and also created a VBA version of the formula i.e.
Code:
Dim i As Long

For i = 2 To 16 Step 1
    Sheet1.Cells(i, 4).Value = Application.WorksheetFunction.MaxIfs(Sheet1.Range(.Cells(2, 1), .Cells(16, 1)), _
                                .Range(.Cells(2, 2), .Cells(16, 2)), .Cells(i, 2).Value, _
                                .Range(.Cells(2, 3), .Cells(16, 3)), .Cells(i, 3).Value)
Next i
I am able to run the above VBA code in about 10 seconds through my entire data set (15k rows) but need the VBA code to run on some older versions of Office and just realised that MAXIFS is supported only in Excel available with Office365 (with subscription) and not in any of the non-subscription versions of Office:confused:

Request:

Can someone please kindly help in coming up with a VBA version of an alternative for MAXIFS that can be used for a data set of over 15k records in ~10-15 seconds?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This should do what you are looking for.

Code:
Sub nMax()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim LR As Long: LR = Range("A" & Rows.Count).End(xlUp).Row()
Dim R As Range: Set R = Range("D2:D" & LR)
Dim cel As Range


For Each cel In R
    cel.Value = Application.Evaluate("=MAX(IF(($B$2:$B$" & LR & "=B" & cel.Row & ")*($C$2:$C$" & LR & "=C" & cel.Row & "),$A$2:$A$" & LR & "))")
Next cel


Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub


Honestly though, I would either just create a PivotTable or go through PowerQuery to sum these calculations up. Both of these options can quickly do the same thing and would probably be faster than VBA and would organize the data to be more readable.
 
Upvote 0
Solution
Thanks lrobbo314, for the quick response.

The code that you shared does not seem to be working and I think the reason is that the formula
(=MAX(IF(($B$2:$B$" & LR & "=B" & cel.Row & ")*($C$2:$C$" & LR & "=C" & cel.Row & "),$A$2:$A$" & LR & "))
needs to be used with CTRL+SHIFT+ENTER and I am not sure if one can use it directly with Application.Evaluate in its current form.

For this problem, I cannot create a Pivot Table or go through Power Query as I need to use the results in Column D for further calculations/lookups in other parts of my VBA code.

Suggestions please?
 
Upvote 0
I don't know why it wouldn't be working. I used the data you posted originally and the code output the values into column D that matched column D in the post. Wrapping a formula in the 'Evaluate' function will resolve array formulas.
 
Upvote 0
Apologies lrobbo314, your solution works perfectly!!

I might have done something wrong earlier. I am currently modifying your solution to expand it for my entire data set.
Will post back with processing times etc.

Thanks a ton!!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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