Finding Min/Max based on multiple criteria (NOT USING ARRAY)

bmd205

New Member
Joined
Nov 12, 2015
Messages
1
Hello-

I've been searching forums all morning trying to find a way to find a way to get the minimum value using two criteria. Here's a sample of what my data looks like, and ideally what I'd like to see as a result.

Case #
Size
Date
1
Small
201501
1
Small
201512
1
Large
201503
1
Large
201508
1
Large
201511
2
Large
201507
2
Large
201511
2
Small
201503
2
Small
201508
2
Small
201501

<tbody>
</tbody>

Ideal output:
Case #
Size
Min
Max
1
Small
201501
201512
1
Large
201503
201511
2
Small
201501
201508
2
Large
201507
201511

<tbody>
</tbody>

I am familiar with Arrays, and I have done a Max(if( nested statement to get the results I need, but I'm also building a macro that will loop through my output (could be 100k+ rows) and the Array Formula just doesn't seem efficient.

Is there a way to write some VBA code to loop through and find the min based on two criteria (Case # and Size)??
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try this:-
Results start "E1"
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Nov19
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Ray [COLOR="Navy"]As[/COLOR] Variant, Q [COLOR="Navy"]As[/COLOR] Variant, K [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] oMax [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] oMin [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Ray = ActiveSheet.Range("A1").CurrentRegion
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] Rw = 2 To UBound(Ray)
    [COLOR="Navy"]If[/COLOR] Not .Exists(Ray(Rw, 1) & "," & Ray(Rw, 2)) [COLOR="Navy"]Then[/COLOR]
        ReDim nray(1 To UBound(Ray, 1))
        nray(1) = Ray(Rw, 3)
        .Add Ray(Rw, 1) & "," & Ray(Rw, 2), Array(nray, 1)
    [COLOR="Navy"]Else[/COLOR]
        Q = .Item(Ray(Rw, 1) & "," & Ray(Rw, 2))
        Q(1) = Q(1) + 1
        Q(0)(Q(1)) = Ray(Rw, 3)
       .Item(Ray(Rw, 1) & "," & Ray(Rw, 2)) = Q
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
ReDim r(1 To UBound(Ray, 1), 1 To 4)
r(1, 1) = "Case #": r(1, 2) = "Size": r(1, 3) = "Min": r(1, 4) = "Max"
c = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    c = c + 1
    r(c, 1) = Split(K, ",")(0)
    r(c, 2) = Split(K, ",")(1)
    r(c, 3) = Application.Min(.Item(K)(0))
    r(c, 4) = Application.Max(.Item(K)(0))
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
Range("E1").Resize(c, 4) = r
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,217
Members
448,951
Latest member
jennlynn

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