繁体   English   中英

我正在尝试将值写入数组并找到数组的最大值?

[英]I am trying to write value to array and find maximum value of array?

如何为数组赋值并从数组值中找到最大值

Sub maxpain()

'name

sym = Sheets("Analysis Ochain").Range("A5").Value

'Column number

ccol = Application.WorksheetFunction.Match(sym & "COI", Sheets(6).Range("1:1"), 0)
pcol = Application.WorksheetFunction.Match(sym & "POI", Sheets(6).Range("1:1"), 0)

'Find last filed cell in respect of column number

last = Sheets(6).Columns(ccol).End(xlDown).Row - 1

'Define array from 2 to last filled cell

Dim accmu(2 To last) As Integer

'Sum of both column and assign value to array

For i = 2 To last
accmu(i) = Sheets(6).Cells(i, ccol).Value + Sheets(6).Cells(i, pcol).Value
Next


'Get maximum value of array 

max = ??????  


End Sub

在数组上使用WorksheetFunction.Max

max = WorksheetFunction.Max(accmu)

另请注意,您不能将Dim accmu(2 to last) as Integer - 您不能在声明中使用变量。 你需要ReDim

Dim accmu() as Integer
Redim accmu(2 to last)

编辑:如果你想找到数组的哪个元素是最大值,只需使用Match

max = WorksheetFunction.Max(accmu)
Debug.Print Application.Match(max, accmu, 0)  <~ returns the element number of max

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM