2nd lowest of 4, middle of 3, lowest of 2, 1 of 1

jmk1153

New Member
Joined
Jun 27, 2018
Messages
14
Hello,

I'm hoping this isn't too complicated but here's what I'm trying to accomplish with no avail.

I have numbers in rows B, C ,D and E. I am trying to write a macro in excel that will populate column F based on the following:

2nd lowest number if there are 4 non-zero numbers, the middle number if 3 non-zero numbers, the lowest of 2 numbers if there are 2 non-zero numbers, 1 of 1 if there is only one non-zero number.

If it's too complicated to consider non-zero numbers, I could always replace 0s with a blank cell beforehand to make that easier.

The one caveat is duplicate numbers would need to count towards the formula i.e. 5/5/5/4 would result in a value of 5, not 4.

5
5
5 - 2nd lowest of 4
4

e.g.


ABCDEF - result
Jim
5​
5​
5​
4​
5​
Paige
1​
3​
2​
0​
2​
Joseph
0​
0​
1​
2​
1​
Carl
1​
1​
2​
0​
1​
Kim
4​
3​
2​
1​
2​
Joe
0​
1​
1​
1​
1​
Chase
4​
4​
0​
0​
4​
Paul
0​
3​
0​
0​
3​
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Book2
ABCDEFG
1Score1Score2Score3Score4Result
2Jim55545
3Paige13202
4Joseph00121
5Carl11201
6Kim43212
7Joe01111
8Chase44004
9Paul03003
10A54212
11B13433
12C53203
13D43222
14E42232
15F45133
16G43033
17H55133
18I11411
19J51511
20K02544
21L40022
22M53055
23N45133
24O54222
25
Sheet2
Cell Formulas
RangeFormula
F2:F24F2=SMALL(B2:E2,CHOOSE(COUNTIF(B2:E2,0)+1, 2,3,3,4))
 
Upvote 0
Is this it?

Code:
Sub jmk1132()
Dim lr, i As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lr
 If WorksheetFunction.CountIf(Range("B" & i & ":E" & i), 0) = 0 Then
   Cells(i, "F") = WorksheetFunction.Small(Range("B" & i & ":E" & i), 2)
 ElseIf WorksheetFunction.CountIf(Range("B" & i & ":E" & i), 0) = 1 Then
 Cells(i, "F") = WorksheetFunction.Large(Range("B" & i & ":E" & i), 2)
 ElseIf WorksheetFunction.CountIf(Range("B" & i & ":E" & i), 0) = 2 Then
   Cells(i, "F") = WorksheetFunction.Small(Range("B" & i & ":E" & i), 3)
 Else
   Cells(i, "F") = WorksheetFunction.Small(Range("B" & i & ":E" & i), 4)
 End If
Next i
End Sub
 
Upvote 0
Solution
Book2
ABCDEFG
1Score1Score2Score3Score4Result
2Jim55545
3Paige13202
4Joseph00121
5Carl11201
6Kim43212
7Joe01111
8Chase44004
9Paul03003
10A54212
11B13433
12C53203
13D43222
14E42232
15F45133
16G43033
17H55133
18I11411
19J51511
20K02544
21L40022
22M53055
23N45133
24O54222
25
Sheet2
Cell Formulas
RangeFormula
F2:F24F2=SMALL(B2:E2,CHOOSE(COUNTIF(B2:E2,0)+1, 2,3,3,4))
Thank you. I didn't realize excel function would be able to handle that!
 
Upvote 0
Is this it?

Code:
Sub jmk1132()
Dim lr, i As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lr
 If WorksheetFunction.CountIf(Range("B" & i & ":E" & i), 0) = 0 Then
   Cells(i, "F") = WorksheetFunction.Small(Range("B" & i & ":E" & i), 2)
 ElseIf WorksheetFunction.CountIf(Range("B" & i & ":E" & i), 0) = 1 Then
 Cells(i, "F") = WorksheetFunction.Large(Range("B" & i & ":E" & i), 2)
 ElseIf WorksheetFunction.CountIf(Range("B" & i & ":E" & i), 0) = 2 Then
   Cells(i, "F") = WorksheetFunction.Small(Range("B" & i & ":E" & i), 3)
 Else
   Cells(i, "F") = WorksheetFunction.Small(Range("B" & i & ":E" & i), 4)
 End If
Next i
End Sub
Yes, this code worked like a charm. Thank you for the assistance!
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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