How to Sort a String Array in VBA

Written by jaime avelar | 13/05/2017
How to Sort a String Array in VBA
Learn how to write VBA code in Excel. (Jupiterimages/ Images)

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 need

  • Microsoft Excel

Show MoreHide

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.