DISCOVER
×

How to Find the Largest Value in an Array in Visual Basic

Updated April 17, 2017

A Visual Basic array is a data structure that allows you to store a list of values. Arrays can store any data type in Visual Basic, from integers to strings. Visual Basic does not provide a function that will automatically find the largest value in an array, but if your array stores numerical values such as integers or floating point numbers, you can program a loop that will search through the array for the largest value and output the result.

Create a variable “Max” that will store the index location of the largest value in the array and initialise it to “0.” Create a variable “i” that will store the current index position in the array.

Create a “For” loop. Have the loop iterate from “i=1” until it reaches the end of the array. Use the “UBound()” function on your array to return the number of items in the array; this is the number of iterations the loop will need to perform.

Compare the value of the array at “Max” to the value of the array at “i.” If the value stored in “Max” is larger, continue with the loop. If the value at “i” is larger, then set “Max” equal to “i” and continue with the loop.

Close the loop.

Create a variable “MaxValue” that will store the highest value in the array. Set “MaxValue” equal to the value stored at “Max” in the array.

Output the value of “MaxValue.”

Tip

Program the loop as its own function so you can reference it multiple times in your program without having to reprogram the entire loop again.

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

William Nagel is a graduate of the Georgia Institute of Technology, where he studied science, technology and culture. He has been writing since 2007 and specializes in computer hardware, operating systems and software documentation. His work has been published in the "North Avenue Review."