LoginSignup
0
0

More than 5 years have passed since last update.

MaxIfsを使ったユニークソート

Last updated at Posted at 2018-05-26

MaxIfsについて

WorksheetFunction の maxifsを使うと簡単にユニークソートが出来る事に。

同じテーブルのR列をソートして結果をU列 (=TSLU([R],[U]))に表示します。

Function TSLU(R, U, Optional S = "") ' TABLE SORT LARGE UNIQ
    On Error Resume Next
    Dim M: M = MM(U)
    Dim J: J = DI(U, M - 1)

    S = IFC(M = 1, WorksheetFunction.Max(R) _
                 , WorksheetFunction.MaxIfs(R, R, CC("<", J)))
    TSLU = IFZS(S)
End Function

但し、R列に0を含む場合はSPACEになって、出力結果が正しくないので、以下の処理を足します。

    If S = 0 Then
       If MI(R, 0) = "" Then
          S = ""
       Else
          If M > 1 And (J <= S Or J = "") Then S = ""
       End If
    End If

長いので、一行にまとめて、最終的には、以下のようになります。

Function TSLU(R, U, Optional S = "") ' TABLE SORT LARGE UNIQ
    On Error Resume Next
    Dim M: M = MM(U)
    Dim J: J = DI(U, M - 1)

    S = IFC(M = 1, WorksheetFunction.Max(R) _
                 , WorksheetFunction.MaxIfs(R, R, CC("<", J)))

    TSLU = IFZS(S, IFNC(MI(R, 0) = "", IFNC(M > 1 And (J = "" Or J <= S), 0)))
End Function

Function IFNC(C, V, Optional S = "")    ' IF NOT C THEN V ELSE S
    If C Then IFNC = S Else IFNC = V
End Function

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0