見出し画像

min が効かない

日付の配列arrをつくり、最も早い日付を求めようと application.worksheetfunction.min(arr) と書いて何度やっても、0が返されてしまいます。配列の要素数は数百オーダー。
同じく最も遅い日付を求める application.worksheetfunction.max(arr) はちゃんとした答えが返ってきます。
どういうことなんでしょう?

しようがないから、small関数を使って並べ替えをして、最も早い日付は1番目の値、ということで切り抜けました。small関数も要素数が多いと効かなくなるので、以下のfunctionをつくっています。入れ子のfunctionはいずれ解説します。これは要素数が多すぎる場合、small関数が効かなくなるため、2次元にして、1行ずつsmall関数で並べ替えをして、再度1次元に戻し、不要な0を省略する、というものです。



Function arrsmall_long(ByVal arr As Variant)
'arrsmall_long:1次元の配列で、0以外の数値を小さい順に並べる  要素数変更なし long

Dim a As Long, b As Long, C As Long, d As Long, e As Long, f As Long, g As Long
Dim h As Long, i As Long, j As Long, k As Long, l As Long, m As Long, n As Long
Dim o As Long, p As Long, q As Long, r As Long, s As Long, t As Long, u As Long
Dim v As Long, w As Long, x As Long, y As Long, z As Long


Dim aa As Long, bb As Long, cc As Long, dd As Long, ee As Long, ff As Long, gg As Long
Dim hh As Long, ii As Long, jj As Long, kk As Long, ll As Long, mm As Long, nn As Long
Dim oo As Long, pp As Long, qq As Long, rr As Long, ss As Long, tt As Long, uu As Long
Dim vv As Long, ww As Long, xx As Long, yy As Long, zz As Long
Dim maxnum As Long
Dim chk() As Long
Dim chk2nd() As Long
Dim chk3rd() As Long
Dim chk4th() As Long
Dim chk5th() As Long

Dim arr2nd() As Long
Dim arr3rd() As Long
Dim arr4th() As Long
Dim arr5th() As Long
Dim arr6th() As Long
Dim arr7th() As Long
Dim chksmall() As Long

ff = 1000
aa = UBound(arr, 1)

ReDim arr2nd(1 To 1)
'arrcomp_except_exnum:1次元の配列で0があるものは省略する:small不使用 long
arr2nd = Module10.arrcomp_except_exnum(arr, 0)
maxnum = Application.WorksheetFunction.Max(arr2nd) * 2
gg = UBound(arr2nd, 1)
hh = Int(gg / ff) + 1

If gg <= 10000 Then
ReDim arr4th(1 To gg)
For g = 1 To gg
arr4th(g) = Application.WorksheetFunction.Small(arr2nd, g)
Next g

Else
'-----------------------------------------------------

ReDim arr4th(1 To 1)

'arr1Dto2D_exnum_division : 1次元配列の数値をexnumの幅に分割し、2次元にする。異常値は0
arr4th = Module10.arr1Dto2D_exnum_division(arr2nd, hh)

ReDim arr3rd(1 To 1)
'arr2D_colcomp:2次元の配列で多数の異常値がある場合に、異常値を省略して列数を短縮する。long
arr3rd = Module10.arr2D_colcomp(arr4th, 0)

ee = UBound(arr3rd, 1)
dd = UBound(arr3rd, 2)


'0を異常値に変換
ReDim arr2nd(1 To ee, 1 To dd)
For e = 1 To ee
For d = 1 To dd
If arr3rd(e, d) = 0 Then
arr2nd(e, d) = maxnum
Else
arr2nd(e, d) = arr3rd(e, d)
End If
Next d
Next e


ReDim arr3rd(1 To ee, 1 To dd)
For d = 1 To dd

'複数のeループ 開始------------------------------------

ReDim chk(1 To ee)
For e = 1 To ee
chk(e) = arr2nd(e, d)
Next e

ReDim chk2nd(1 To ee)

For e = 1 To ee
chk2nd(e) = Application.WorksheetFunction.Small(chk, e)
Next e


For e = 1 To ee
arr3rd(e, d) = chk2nd(e)
Next e
'複数のeループ 終了------------------------------------

Next d


'arr2D_rowcomp2nd:2次元の配列で多数の異常値がある場合に、異常値を省略して行数を短縮する。long
'タテ方向をひとまとめに考える
'必要な数値は列の上方(行番号が小さいところ)に固まっている
ReDim arr4th(1 To 1)
arr4th = Module10.arr2D_rowcomp2nd(arr3rd, maxnum)
ReDim arr2nd(1 To 1)
'arr2Dto1D_2nd:2次元の配列を1次元にする。省略なし 配列は整数(long)
'タテ方向をひとまとめに考える
arr2nd = Module10.arr2Dto1D_2nd(arr4th)
ReDim arr4th(1 To 1)
'arrcomp_except_exnum:1次元の配列で異常値を省略し短縮する long
arr4th = Module10.arrcomp_except_exnum(arr2nd, maxnum)

End If

arrsmall_long = arr4th

End Function


この記事が気に入ったらサポートをしてみませんか?