DISCOVER
×

How to Sort a String Array in VBA

Updated July 20, 2017

Sorting a string array in Visual Basic for Applications (VBA) is not as simple as in other, more recent programming languages. In VBA, you can sort an array in ascending order using a couple of "For...Loops" and looping through each element in the array. This method is useful when you need to display values back to the user in ascending order, to give your data a more professional look.

Launch Microsoft Excel, click the "Developer" tab, then click "Visual Basic" to open the VB Editor. Create a new sub procedure by adding the following code:

Private Sub SortVBAArray()

Create your string array and add ten values to it:

Call the sub procedure that will sort the values in the array and end the procedure:

End Sub

Create the sub procedure that will sort the string array in ascending order and display the results through the Immediate window:

Sub sort Array(tmpArray() As String)

End Sub

Click the first sub procedure and press "Ctrl" + "G" to display the Immediate window. Press "F5" to run the program and view the results.

Things You'll Need

  • Microsoft Excel
Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Jaime Avelar is a professional writer whose programming articles appear on various websites. He has been a software programmer since 2000. Avelar holds a Master of Science in information systems from the University of Texas at Arlington.