简体   繁体   中英

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

How can I assign value to array and find max from array value

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

Use WorksheetFunction.Max on the array.

max = WorksheetFunction.Max(accmu)

Also note, you can't Dim accmu(2 to last) as Integer - you can't use a variable within the declaration. You need to ReDim .

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

EDIT: If you want to find which element of the array is the max, just use Match .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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