确定最小值,下标超出范围的错误

第一次海报在这里,业余VBA-ist。 我在Matlab中已经有了过去的代码逻辑经验,但是我发现VBA超越了我。 以下是我用来确定具有多个峰值的特定数据集的局部最小值和最大值的代码。 不幸的是,数据是“嘈杂”的。 意味着有多个局部最大值/最小值,因为我们遵循由于读数波动引起的真正的极值之间的趋势。
我编辑了一些我在其他地方find的代码,我认为它会确定这些真正的最小点(我可以通过何时启动/停止stream程来确定它们),因为最大值从来不是问题。 但是当它到达星号(第二轮lngCnt = lngCnt + 1)时,会出现“运行时错误9:下标超出范围”。 我试图研究这个问题,但是我无法理解要解决的问题,或者看看答案如何适用于我的代码。

这是我使用的代码:

Sub maxmin() Dim X Dim Y Dim lngRow As Long Dim lngCnt As Long X = Range([A2], Cells(Rows.Count, "C").End(xlUp)) 'self defining function for the range over which the data will be analyzed, data in spreadsheet must start in L26 Y = Application.Transpose(X) 'creates a column rather than a row For lngRow = 2 To UBound(X, 1) - 1 'defines the function for the long variable row, to the upper bound of the column If X(lngRow, 3) > X(lngRow - 1, 3) Then 'logic statement to assist in max/min If X(lngRow, 3) > X(lngRow + 1, 3) Then 'logic statement lngCnt = lngCnt + 1 Y(1, lngCnt) = X(lngRow, 1) Y(2, lngCnt) = X(lngRow, 2) Y(3, lngCnt) = X(lngRow, 3) End If Else If X(lngRow - 1, 1) < 100 Then 'this and the following line determine where the min is located based off the change in flow rate If X(lngRow, 1) > 150 Then lngCnt = lngCnt + 1 Y(1, lngCnt) = X(lngRow, 1) Y(2, lngCnt) = X(lngRow, 2) Y(3, lngCnt) = X(lngRow, 3) End If End If End If Next lngRow ReDim Preserve Y(1 To 3, 1 To lngCnt) Range("D2:F4300 ") = Application.Transpose(Y) 'prints my data to desired cells End Sub 

样本数据集(在大约55分钟的时间段内,椭圆表示该趋势的延续)将如下所示:

 0 3000 0 2900 0 2850 0 2825 0 2800 24 2800 23 2775 21 2775 19 2750 170 3400 245 3600 290 3800 290 4000 290 4200 ... 305 11600 175 11800 23 11700 19 11600 20 11500 0 11400 0 11300 0 11200 0 11100 

用这一行索引怎么办给我一个下标错误?

另外,任何人都可以提供一个更好的方式来缩放我的输出数组只需要这个单元格? 我试过使用lngCnt值,但它不起作用。 以前的代码通过使用数组值来代替100和150。

提前感谢您的帮助!

 Range("D2:F4300") = Application.Transpose(Y) 

可能

 Range("D2").resize(Ubound(Y,2), Ubound(Y,1)).Value = Application.Transpose(Y) 

没有转位,你会翻转的界限:

 Range("D2").resize(Ubound(Y,1), Ubound(Y,2)).Value = Y