How To Sorting text data in alphabetical order using VBA in Microsoft Excel?
Tags: tosortingtextdataina, vbamacros
Published: 2021-09-28 04:30:31

---
title: How To Sorting text data in alphabetical order using VBA in Microsoft Excel?
published: true
date: 2021-09-23 09:07:31 UTC
tags: ToSortingTextDataInA,VBAMacros
canonical_url: https://geekexcel.com/how-to-sorting-text-data-in-alphabetical-order-using-vba-in-microsoft-excel/
---

In this tutorial, we will guide you on how to **sort text data in alphabetical order** using VBA in Microsoft Excel. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Example

- Firstly, you need to  **create a sample data** consists of **random names** that we want to **sort**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-35.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Option Explicit
Function SortString(Source As Range, Position As Long) As String
'Declaring variables
Dim Cell As Range
Dim values() As String
Dim i As Long, j As Long
Dim Done As Boolean
ReDim values(1 To 1)
'Looping through all cells in the range
For Each Cell In Source
        Done = False
        i = 1
        'Creating an array of sorted name
        Do
            If Cell.Value < values(i) Then
                Done = True
            Else
                i = i + 1
            End If
        Loop While Done = False And i <= UBound(values) 
        ReDim Preserve values(1 To UBound(values) + 1)   
        If i <= UBound(values) Then
            'Adjusting value in sorted name array
            For j = UBound(values) To i + 1 Step -1
                values(j) = values(j - 1)
            Next j
        End If
        values(i) = Cell.Value
Next Cell
'Assigning value of required position
SortString = values(Position + 1)
End Function

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-72.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Finally, you received the output that have **created SortString custom function**  **to sort data alphabetically**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sorted-Name-Output.png)<figcaption>Sorted Name Output</figcaption>

## Verdict

We hope that this tutorial gives you guidelines on how to **sort text data in alphabetical order** using VBA in Microsoft Excel. Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Related Articles:**

- **[How to Fill Cell References From Multiple Worksheets in Excel?](https://geekexcel.com/how-to-fill-cell-references-from-multiple-worksheets-in-excel/)**
- **[How to Search for a Value in All Tabs in Excel Office 365?](https://geekexcel.com/how-to-search-for-a-value-in-all-tabs-in-excel-office-365/)**
- **[Use COMMAND RIBBON Options in Microsoft Excel 365!](https://geekexcel.com/use-command-ribbon-options-in-microsoft-excel-365/)**
- **[How to Sort Sheets/Worksheets Alphabetically in Excel Office 365?](https://geekexcel.com/how-to-sort-sheets-worksheets-alphabetically-in-excel-office-365/)**
- **[How to Unhide a Worksheet in Microsoft Excel 365?](https://geekexcel.com/how-to-unhide-a-worksheet-in-microsoft-excel-365/)**