'以下數列的來源是由其他程式產生,不在工作表的儲存格。
'此例數列共有 374 個數字,實際上數字列的長度不一定。
'附圖曲線是此例數字列的表示,用以方便說明。
'
Sub FindValley()
Dim aa$, valley(2, 10) '谷的位置&谷的數值
Dim ar, arr
Dim i%
'數字列
aa = "451.8,452.5,453.8,454.4,454.7,455.1,455.6,455.9,457.2,459.1,460.8,463.3,466.7,469.5,471.9,474.2,476.3,"
aa = aa & "478.4,480.1,481.5,482.4,483.3,483.4,484.3,484.9,485.6,485.7,485.8,485.7,485.3,485.3,485.7,485.8,485.9,486.3,"
aa = aa & "486.4,486.7,487.3,488.2,488.6,489.1,489.6,490.1,490.4,489.5,488.6,487.2,485.7,485.6,483.9,483.2,482.1,481.2,"
aa = aa & "479.5,478,477.2,474.5,472.1,467.2,463.3,460.5,458.5,455.1,440.2,430.5,420.3,415.2,400.2,375.1,350.5,336,"
aa = aa & "328.5,320,308.5,306,305,303.5,302.5,301.2,300.5,301.3,302.1,303.5,306.3,307.6,309,315.5,318.7,320.1,328.7,"
aa = aa & "335.6,345.5,350.1,352.5,361.3,364.2,365.8,367.4,368.2,370.5,375.3,380.2,384.7,386.4,390.2,395.4,400.8,"
aa = aa & "408.7,415.2,425.6,440.5,445.8,450.5,457.3,459.1,460.5,462.1,462.3,463.5,465.3,465.5,466,467,467,467.1,467.6,467.8,"
aa = aa & "467.6,467.7,467.8,467.9,467.8,467.4,466.9,466.1,465.7,465.1,464.4,463.9,463.3,462.6,461.6,460.7,460.2,"
aa = aa & "459.4,458.3,457,455.9,454.5,453.2,450.5,447.9,445.2,442.6,440.4,438.3,435.6,432.8,430,427.4,424.6,421.4,"
aa = aa & "417.9,414.5,410.8,407.4,404,401.4,398.8,396.2,393.3,390,386.9,383.9,380.8,"
aa = aa & "377.5,374.3,371,367.7,364.6,362.1,360.3,358.7,357.1,355.4,353.4,351.3,349.6,347.9,346.2,344.4,342.7,341.2,339.7,"
aa = aa & "338.4,336.8,335.5,334,332.3,330.3,328.4,326.6,324.9,323.2,321.7,320.4,319.2,317.7,316.5,315.2,315.1,"
aa = aa & "315.2,315,315,314.9,314.7,314.5,314.3,314.3,314.4,314.7,315.1,315.4,315.7,315.9,316.1,316.2,316.3,316.8,"
aa = aa & "317.3,317.9,318.5,319.2,319.9,320.4,320.9,321.1,320.8,320.2,319.7,319.1,318.6,318.2,317.8,317.4,316.8,316,"
aa = aa & "315.3,314.8,314.4,314,313.6,313.1,312.3,311.6,311,310.3,309.7,309,308.2,307.1,305.9,304.2,302.4,300.5,298.7,"
aa = aa & "296.8,294.8,292.8,290.7,289,287.3,286,284.9,283.7,282.4,281.2,280,278.6,277.2,275.8,274.3,272.6,271.1,269.7,"
aa = aa & "268.1,266.6,265.1,263.3,261.6,259.7,257.4,254.9,252.5,250.1,247.7,245.2,243,240.8,238.7,236.6,234.6,232.7,230.9,"
aa = aa & "229.1,227.2,225.4,223.7,222.1,220.4,218.8,217.3,216.1,215.2,214.4,213.6,212.9,212,211.4,210.8,210.3,209.9,210,"
aa = aa & "210.3,210.6,211.2,211.8,212.7,213.3,213.9,214.5,214.6,214.3,214.4,214.5,214.6,214.7,215,215.5,215.8,216.3,216.9,"
aa = aa & "217.3,217.8,218.6,219.3,220.5,221.8,223,224.1,225.4,"
aa = aa & "226.9,228.4,229.7,231.2,233.1,234.9,236.9,238.8,240.7,242.4,244,245.5,247.1,248.8,250.6,252.3,254.1,255.6,"
aa = aa & "257.2,258.7,259.7,260.6,261.4,262"
'轉成陣列
ar = Split(aa, ",")
ReDim arr(UBound(ar))
For i = 0 To UBound(ar) - 1
arr(i + 1) = CSng(ar(i))
Next
Sub FindValley()
Dim aa$, valley()
Dim ar, Arr
Dim i%
'數字列
aa = "451.8,452.5,453.8,454.4,454.7,455.1,455.6,455.9,457.2,459.1,460.8,463.3,466.7,469.5,471.9,474.2,476.3,"
aa = aa & "478.4,480.1,481.5,482.4,483.3,483.4,484.3,484.9,485.6,485.7,485.8,485.7,485.3,485.3,485.7,485.8,485.9,486.3,"
aa = aa & "486.4,486.7,487.3,488.2,488.6,489.1,489.6,490.1,490.4,489.5,488.6,487.2,485.7,485.6,483.9,483.2,482.1,481.2,"
aa = aa & "479.5,478,477.2,474.5,472.1,467.2,463.3,460.5,458.5,455.1,440.2,430.5,420.3,415.2,400.2,375.1,350.5,336,"
aa = aa & "328.5,320,308.5,306,305,303.5,302.5,301.2,300.5,301.3,302.1,303.5,306.3,307.6,309,315.5,318.7,320.1,328.7,"
aa = aa & "335.6,345.5,350.1,352.5,361.3,364.2,365.8,367.4,368.2,370.5,375.3,380.2,384.7,386.4,390.2,395.4,400.8,"
aa = aa & "408.7,415.2,425.6,440.5,445.8,450.5,457.3,459.1,460.5,462.1,462.3,463.5,465.3,465.5,466,467,467,467.1,467.6,467.8,"
aa = aa & "467.6,467.7,467.8,467.9,467.8,467.4,466.9,466.1,465.7,465.1,464.4,463.9,463.3,462.6,461.6,460.7,460.2,"
aa = aa & "459.4,458.3,457,455.9,454.5,453.2,450.5,447.9,445.2,442.6,440.4,438.3,435.6,432.8,430,427.4,424.6,421.4,"
aa = aa & "417.9,414.5,410.8,407.4,404,401.4,398.8,396.2,393.3,390,386.9,383.9,380.8,"
aa = aa & "377.5,374.3,371,367.7,364.6,362.1,360.3,358.7,357.1,355.4,353.4,351.3,349.6,347.9,346.2,344.4,342.7,341.2,339.7,"
aa = aa & "338.4,336.8,335.5,334,332.3,330.3,328.4,326.6,324.9,323.2,321.7,320.4,319.2,317.7,316.5,315.2,315.1,"
aa = aa & "315.2,315,315,314.9,314.7,314.5,314.3,314.3,314.4,314.7,315.1,315.4,315.7,315.9,316.1,316.2,316.3,316.8,"
aa = aa & "317.3,317.9,318.5,319.2,319.9,320.4,320.9,321.1,320.8,320.2,319.7,319.1,318.6,318.2,317.8,317.4,316.8,316,"
aa = aa & "315.3,314.8,314.4,314,313.6,313.1,312.3,311.6,311,310.3,309.7,309,308.2,307.1,305.9,304.2,302.4,300.5,298.7,"
aa = aa & "296.8,294.8,292.8,290.7,289,287.3,286,284.9,283.7,282.4,281.2,280,278.6,277.2,275.8,274.3,272.6,271.1,269.7,"
aa = aa & "268.1,266.6,265.1,263.3,261.6,259.7,257.4,254.9,252.5,250.1,247.7,245.2,243,240.8,238.7,236.6,234.6,232.7,230.9,"
aa = aa & "229.1,227.2,225.4,223.7,222.1,220.4,218.8,217.3,216.1,215.2,214.4,213.6,212.9,212,211.4,210.8,210.3,209.9,210,"
aa = aa & "210.3,210.6,211.2,211.8,212.7,213.3,213.9,214.5,214.6,214.3,214.4,214.5,214.6,214.7,215,215.5,215.8,216.3,216.9,"
aa = aa & "217.3,217.8,218.6,219.3,220.5,221.8,223,224.1,225.4,"
aa = aa & "226.9,228.4,229.7,231.2,233.1,234.9,236.9,238.8,240.7,242.4,244,245.5,247.1,248.8,250.6,252.3,254.1,255.6,"
aa = aa & "257.2,258.7,259.7,260.6,261.4,262"
'轉成陣列
ar = Split(aa, ",")
ReDim Arr(UBound(ar))
For i = 0 To UBound(ar) - 1
Arr(i + 1) = CSng(ar(i))
Next
'後一值減前一值陣列-Brr
ReDim Brr(UBound(Arr))
For i = 2 To UBound(Arr)
Brr(i) = Arr(i) - Arr(i - 1)
Next
'找極值位置
For i = 2 To UBound(Brr) - 1
If Brr(i) >= 0 And Brr(i + 1) < 0 Then 峰位 = 峰位 & "," & i
If Brr(i) < 0 And Brr(i + 1) >= 0 Then 谷位 = 谷位 & "," & i
Next
'用位置陣列得數值陣列
ReDim 峰值Arr(UBound(峰位Arr))
For i = 0 To UBound(峰位Arr)
峰值Arr(i) = Arr(峰位Arr(i))
Next
ReDim 谷值Arr(UBound(谷位Arr))
For i = 0 To UBound(谷位Arr)
谷值Arr(i) = Arr(谷位Arr(i))
Next
For i = 0 To UBound(谷位Arr)
valley(2, i + 1) = 谷位Arr(i) '位置
valley(1, i + 1) = Arr(谷位Arr(i)) '數值
Next
For i = 1 To UBound(valley, 2)
Debug.Print "第" & i & "低谷數值=" & valley(1, i) & " ; 第" & i & "低谷位置=" & valley(2, i)
Next
End Sub
Function ArrSortBrr(ByVal Arr, Order, ByVal Brr)
'用Arr陣列的資料順序來排序Brr
For i = LBound(Arr) To UBound(Arr) - 1
For j = i + 1 To UBound(Arr)
If Order Then '大 > 小
If Arr(j) > Arr(i) Then
T = Arr(i): Arr(i) = Arr(j): Arr(j) = T
T = Brr(i): Brr(i) = Brr(j): Brr(j) = T
End If
Else ' 小 > 大
If Arr(j) < Arr(i) Then
T = Arr(i): Arr(i) = Arr(j): Arr(j) = T
T = Brr(i): Brr(i) = Brr(j): Brr(j) = T
End If
End If
Next j
Next i
ArrSortBrr = Brr
End Function作者: quickfixer 時間: 2020-7-30 00:47