Re: Trying to write "2nd highest" score in addition to MaxScore.
Holy crap! It works! With 8 columns it adds the 1+5,2+6,3+7,4+8 then displays the 2nd largest of first largest, etc!!!!
Since you put some real effort in this is there any way you could spend just a few more minutes and add some comments 're: what does what?
This type of code I'm not so familiar with. Moreso than straight formula code though! lol. Formula code, to me, is along the lines of the two accounting classes I took at University. I could never pull better than a C+. And this coming from someone that scored the 2nd highest Calc Final one semester among a couple thousand students!
Quote from Krishnakumar;790628Display MoreProbably a UDF..
Hit Alt+F11 > goto Insert > Module and paste the following code there. Then hit Alt+Q to close the VBE window.
Now in the worksheet, use the formula as MAXNTH(A1:H1,2)
VB:
Option Explicit
Function MAXNTH(NumRange As Range, Optional Nth As Long = 1)
Dim k, kk, i As Long, c As Long, r As Long
c = NumRange.Columns.Count
r = NumRange.Rows.Count
If c > 1 And r > 1 Then
MAXNTH = CVErr(xlErrNum)
Exit Function
End If
If c > 1 Then
If Nth > c Then
MAXNTH = CVErr(xlErrNum)
Exit Function
End If
k = NumRange.Parent.Evaluate("transpose(transpose(" & NumRange.Address & "))")
ElseIf r > 1 Then
If Nth > r Then
MAXNTH = CVErr(xlErrNum)
Exit Function
End If
k = NumRange.Parent.Evaluate("transpose(" & NumRange.Address & ")")
End If
With CreateObject("scripting.dictionary")
.comparemode = 1
For i = 1 To UBound(k)
kk = Split(Replace(k(i), ")", vbNullString), "(")
.Item(kk(1)) = .Item(kk(1)) + CDbl(kk(0))
Next
c = .Count
If c Then
k = .keys: kk = .items
If Nth > c Then
MAXNTH = CVErr(xlErrNum)
Exit Function
End If
c = Application.Large(kk, Nth)
r = Application.Match(c, kk, 0)
MAXNTH = c & "(" & k(r - 1) & ")"
End If
End With
End Function