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
The VBA Max Functions
The VBA Max Functions. ... VBA's Max function returns the maximum value from a list of values you enter. For example, "worksheetfunction.max(1,...
What Is UBound and LBound in Visual Basic?
How to Find the Maximum Value in an Array in Visual Basic; How to Find the Largest Value in an Array in...
How to Find the Maximum in Array MATLAB
The "max" function is used to determine the maximum value in a MATLAB array or to find the maximum element in each...
How to Make a Flowchart for a Program With an Array
How to Make a Loop Flow Chart in Visual Logic. Visual Logic is a software program designed for logic analysis. ... How...
How to Calculate Price Per Share of Common Stock
Stock analysts use several methods to calculate price per share of many stocks using similar techniques for ... How to Find Out...
How to Find the Largest Number in an Array
How to Find the Largest Number in an Array. Computer programming can be a difficult task. ... How to Find the Largest...
How to Find a Character in a String in Visual Basic
How to Find a Character in a String in Visual Basic. ... How to Find the Number of Occurrences of a ......
VBA: How to Determine an Array Size
How to Determine the Maximum Disk Size of an IDE RAID Controller. ... Sorting a string array in Visual Basic for Applications...
How to Assign Array Values to a Range in Excel 2007
How to Assign Array Values to a Range in Excel 2007. Excel array formulas are a shortcut method for dealing with multiple...