This could be a start:-
Code (1) first loop through the range of cell to initialise all the variables. (The variables are 10 different numbers in Range ("A1:A10")) NB:- You could skip this first bit if you want to assign your variables in a different way It then loops through the Array, by setting the first number as Max and then, if the next number in array is larger than the previous then that becomes the Max .
Private Sub CommandButton1_Click()
Dim n(1 To 10) As Integer, a As Integer
Dim mx As Integer, x As Integer
a = 1
Do Until Cells(a, 1).Value = ""
n(a) = Cells(a, 1).Value
a = a + 1
Loop
mx = n(1)
For x = 1 To 10
If x > 1 Then
If n(x) > mx Then
mx = n(x)
End If
End If
Next x
MsgBox mx
End Sub
You can also set the Array as a single Variant, variable as shown in the second bit of code, and then use the Worksheetfunction.Max.
NB:- Variant arrays always have two Indices as shown
Private Sub CommandButton2_Click()
Dim n As Variant
n = Range("a1:A10").Value
MsgBox WorksheetFunction.Max(n)
MsgBox n(1, 1) 'This is just to show the first variable
End Sub
I hope this helps
Mick
Bookmarks