0% found this document useful (0 votes)
2 views

VBA for Sorting Names in Classlists

This document provides a VBA script for sorting names in alphabetical order and converting them to sentence case in an Excel worksheet. It outlines the steps to set up the script, including determining the last row of data, formatting names, and sorting them. Instructions for using the script in the Excel VBA editor are also included.

Uploaded by

Andrew Goya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

VBA for Sorting Names in Classlists

This document provides a VBA script for sorting names in alphabetical order and converting them to sentence case in an Excel worksheet. It outlines the steps to set up the script, including determining the last row of data, formatting names, and sorting them. Instructions for using the script in the Excel VBA editor are also included.

Uploaded by

Andrew Goya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

VBA for Sorting Names in Classlists

Here's a VBA script that will help you automatically sort names in
alphabetical order and convert them to sentence case in your Excel
worksheet.

Assume you have a worksheet with the following data structure:

- Column A: Names

Here's the VBA script:

```vba

Sub SortAndFormatNames()

Dim ws As Worksheet

Dim lastRow As Long

Dim i As Long

Dim nameRange As Range

Dim cell As Range

Dim name As String

' Set your worksheet

Set ws = ThisWorkbook.Sheets("Sheet1")

' Determine the last row with data

lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Set the range containing the names


Set nameRange = ws.Range("A2:A" & lastRow) ' Assuming headers are in
row 1

' Convert names to sentence case

For Each cell In nameRange

name = StrConv(cell.Value, vbProperCase)

cell.Value = name

Next cell

' Sort the names in alphabetical order

nameRange.Sort Key1:=ws.Range("A2"), Order1:=xlAscending,


Header:=xlNo

MsgBox "Names have been sorted and formatted successfully!"

End Sub

```

Here's what the script does:

1. Sets the worksheet containing the data.

2. Determines the last row with data in Column A.

3. Converts each name to sentence case (i.e., first letter of each word in
uppercase).

4. Sorts the names in alphabetical order.

5. Displays a message box once the process is complete.

To use this script:

1. Open Excel and press `Alt + F11` to open the VBA editor.

2. Insert a new module (`Insert` > `Module`).


3. Copy and paste the script into the module.

4. Close the VBA editor.

5. Press `Alt + F8`, select `SortAndFormatNames`, and click "Run".

This will automatically sort the names and format them to sentence case in
your specified worksheet.

You might also like