使用VBA查找循环的最大值

我试图find一个循环的最大值。 首先,我有两个随机数组,我想要find这两个数组的相关系数。 那么,我想多次计算它“I3”单元格。 之后,我想写一个代码,从这个计算中find最大的相关系数。 我写了下面的代码,但没有奏效。

Sub Macro1() Dim i As Long For i = 1 To Range("I3") Calculate Next DMax = Application.WorksheetFunction.Max("A2") Range("I4").Value = DMax End Sub 

任何帮助表示赞赏。

你的最大function需要一个适当的参数。 只需input“A2”在VBA中不起作用。 尝试:

 DMax = Application.WorksheetFunction.Max(Range("A2")) 

这会给你数组A2的最大值。 但请记住,由单个单元格组成的范围的最大值始终是单元格的值。


如果要计算所有迭代的最大值,则应在每次迭代(在for循环内)使用max函数并存储它的值。 在接下来的每个迭代中,如果新的最大值大于旧值,则应该覆盖最大值。 像这样:

 Sub Macro1() Dim i As Long DMax = 0 For i = 1 To Range("I3") Calculate DMax2 = Application.WorksheetFunction.Max(Range(...)) If DMax2 > DMax Then DMax = DMax2 Next i Range("I4").Value = DMax 

这会给你所有迭代的范围(…)的最大值。

我几乎不了解你的代码,但是解决scheme将被循环播放。 假设您有两组数字: A2Cells(2, 1) )到I2Cells(2, 7) )和A3Cells(3, 1) )到I3Cells(3, 7) I3 Cells(3, 7) )。 你想计算偏相关,并find它的最大值。

 For i = 1 To 7 For j = 1 To i 'Calculate the correlation Next j 'here you have partial coefficient and you can compare it, 'if it is greater than previous one then save it and store Next i 
 For i = 1 To Range("I3").value 'to calculate from 1 to the value in that cell 

我会推荐你​​的问题。

 For i = 1 To 10 ' loop 10 times For j = 1 To i ' here it will allow you to check your stuff multiple times before moving on to the next value arr1(i) = arr2(j) ' see if your array match Next j Next i