Get minimum value from table using criteria

Zenru

New Member
Joined
Oct 19, 2017
Messages
29
Hello,

I have a table which holds some important information, the columns I need to work with are:

Parent
Size
Units


ParentSizeUnits
HOLLOWCOAB, AS2, AR3, AT38717
HOLLOWCOAS2, AR3875
HOLLOWCOAT38784
NUDISAT345

<tbody>
</tbody>

I need a macro that will search in Parent and Size and pull the lowest value found in units. Notice Size has multiple "sizes" concatenated by a comma, I need to be able to individually search each size until a match is found there as well.

The data by which I will do a search goes like this:

ParentC,
SizeC (one size only)
UnitsC (the lowest value in Units)

ParentCSizeCUnitsC
HOLLOWCOAT38717
HOLLOWCOAS2875
HOLLOWCOAR3875
NUDISAT345

<tbody>
</tbody>

There are some UDF that gives the MINIF and Excel array formula can do the same, but I cant find a way to individually search each size in the first column and find a match.

I am extremely confused in this, if anyone has any idea as to how to deal with this please feel free to share.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Row\Col
A​
B​
C​
D​
E​
F​
G​
H​
1​
ParentSizeUnitsParentCSizeCUnitsC
2​
HOLLOWCOAB, AS2, AR3, AT3
8717
HOLLOWCOAT3
8717
3​
HOLLOWCOAS2, AR3
875
HOLLOWCOAS2
875
4​
HOLLOWCOAT3
8784
HOLLOWCOAR3
875
5​
NUDISAT3
45
NUDISAT3
45

In H2 control+shift+enter, not just enter, and copy down:

=MIN(IF($A$2:$A$5=$F2,IF(ISNUMBER(SEARCH($G2&",",$B$2:$B$5&",")),$C$2:$C$5)))
 
Upvote 0
By the way, if MINIFS is available on your system...

In H2 just enter and copy down:

=MINIFS($C$2:$C$5,$A$2:$A$5,$F2,$B$2:$B$5,"*"&$G2&"*")
 
Upvote 0
Sadly, MINIF is not allowed. Now, if there is a VBA way to do this I would prefer it. I have bad experiences with performance using arrays. Still, your array looks simple enough so it shouldn’t cause any problems. Thanks!
 
Upvote 0
.. if there is a VBA way to do this I would prefer it.
If the layout was like this
Excel Workbook
ABCDEFGH
1ParentSizeUnitsParentCSizeCUnitsC
2HOLLOWCOAB, AS2, AR3, AT38717HOLLOWCOAT3
3HOLLOWCOAS2, AR3875HOLLOWCOAS2
4HOLLOWCOAT38784HOLLOWCOAR3
5NUDISAT345NUDISAT3
Min Value


Then try
Code:
Sub MinValue()
  Dim d As Object
  Dim a As Variant, b As Variant, e As Variant
  Dim i As Long
  
  a = Range("A1").CurrentRegion.Value
  b = Range("F1").CurrentRegion.Value
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  For i = 2 To UBound(a)
    For Each e In Split(a(i, 2), ", ")
      If d.exists(a(i, 1) & "|" & e) Then
        If a(i, 3) < d(a(i, 1) & "|" & e) Then d(a(i, 1) & "|" & e) = a(i, 3)
      Else
        d(a(i, 1) & "|" & e) = a(i, 3)
      End If
    Next e
  Next i
  For i = 2 To UBound(b)
    If d.exists(b(i, 1) & "|" & b(i, 2)) Then
      b(i, 3) = d(b(i, 1) & "|" & b(i, 2))
    Else
      b(i, 3) = "N/A"
    End If
  Next i
  Range("F1").Resize(UBound(b), UBound(b, 2)).Value = b
End Sub

Result for me:

Excel Workbook
ABCDEFGH
1ParentSizeUnitsParentCSizeCUnitsC
2HOLLOWCOAB, AS2, AR3, AT38717HOLLOWCOAT38717
3HOLLOWCOAS2, AR3875HOLLOWCOAS2875
4HOLLOWCOAT38784HOLLOWCOAR3875
5NUDISAT345NUDISAT345
Min Value
 
Upvote 0
Sadly, MINIF is not allowed. Now, if there is a VBA way to do this I would prefer it. I have bad experiences with performance using arrays. Still, your array looks simple enough so it shouldn’t cause any problems. Thanks!

You are welcome. Yes, it's simple.
 
Upvote 0
Yes, it's simple.
I agree that a formula is simpler & only provided macro since the OP asked for it. However, your formula may be a little too simple and need a small amount of extra complexity by adding some more 'boundaries' around the values. I don't know if it would be possible with the OP's data but if cell B2 is altered to "AB, AS2, AR3, VAT3", your formulas still reports 8717 in H2 when I presume it should then be 8784.
 
Last edited:
Upvote 0
Thank you, Peter! I used your code but I modified it a bit and it worked perfectly!

Now this part:

Code:
Range("F1").Resize(UBound(b), UBound(b, 2)).Value = b

Really blew up my mind and I am still not sure how that works. I am sure I will figure it out myself in a while.

Thanks again, Aladin, your formula suits my needs at the moments but like Peter, I am not confident enough to say I know all possible Size data. That is why I wanted a macro.
 
Upvote 0
Thank you, Peter! I used your code but I modified it a bit and it worked perfectly!
You're welcome. Glad you could modify to suit your needs.

Code:
Range("F1").Resize(UBound(b), UBound(b, 2)).Value = b

Really blew up my mind and I am still not sure how that works. I am sure I will figure it out myself in a while.
Best to figure it out yourself, but if you don't, & want further explanation, post back.


BTW, a warning: My code relies on the column B values being delimited by a comma and a space like your samples, even though your original description only mentioned a comma. If there is any variation in the format of column B, the code may need some more tweaking.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,666
Members
448,977
Latest member
moonlight6

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