Arrays are variables used in programming to hold a list of numbers or characters. Finding the maximum number value in an array requires you to loop through each value. This is also used in a method called "bubble sorting," which means each value is compared and listed in increasing order. You can use a bubble sort to find the maximum value for an array in Visual Basic.
Initialize the variables needed. There are three variables needed to loop through the array to find the highest value: the array that contains the values, a temporary variable to loop through each array value and a variable to hold the highest value. These variables are initialized using the following code:
Dim myArray (2) as Integer
Dim i as Integer
Dim maxValue as Integer
myArray(0) = 1
myArray(1) = 2
myArray(2) = 3
Notice the array is initialized with numbers, 1, 2 and 3. After the code is finished executing, the maximum value shown is 3.
Obtain the first value in the array and set it to the maxValue variable. This variable is used to compare each array value. If the next value in the array list is higher than the current value, then maxValue holds the maximum number. The code is below:
maxValue = myArray(0)
Loop through each value in the array to find the maximum value. The code is below:
For i = 0 To myArray.GetUpperBound() - 1
If maxValue < myArray(i) Then
maxValue = myArray(i)
Print the results to the debugger window. This is used to test your code syntax and logic for any errors. The following code prints "3" after the loop is finished:
- Photo Credit Comstock Images/Comstock/Getty Images
How to Find a Character in a String in Visual Basic
Finding a character in a string can seem like a pretty daunting task if you don't know the right tools to use....
How to Find the Largest Number in an Array
Computer programming can be a difficult task. Using Java is no different, especially if you're looking for something as complex as an...
VBA: How to Determine an Array Size
Using arrays in Visual Basic for Applications, or VBA, procedures efficiently manages large amounts of data, and saves memory and execution time....
How to Find the Maximum in Array MATLAB
Matrices and vectors are the essential mathematical structures used in MATLAB. From a computational perspective, however, matrices and vectors are simply different...
What Is UBound and LBound in Visual Basic?
Incorporating arrays in your Visual Basic (VB) application can become very handy if you know how to use them. VB is a...
The VBA Max Functions
The VBA Max functions are Max, MaxA, and Dmax, all of which your VBA programs can access from Excel's programming environment. You...