Vba Code to Sort Data

We are going to learn how to sort data in columns with VBA instead of using Excel’s sorting feature. We are going to random sort the data in columns using VBA.

Random numbers

To do this follow these steps:

First write any 5 random names in column A. Now you are going to write 5 random numbers to column B.

The ribbon

Now place a command button on your Excel sheet. To open the developer tab right-click anywhere on the ribbon (the top menu) and select “Customize the Ribbon” from the drop down menu.

Customize The Ribbon

Click on the Developer check box. It is under “Customize the Ribbon Main Tabs”.

Add Developer Tab

The developer tap is now present next to the view tab on the top menu bar. Click on the DEVELOPER tab and select “Insert”.

Insert Controls

The button

Select the Command button in the ActiveX Controls.

ActiveX controls

Drag the command button on your Excel worksheet. Right-click on the command button (make sure Design Mode is selected) and go to properties. Write Rand Sort Names in the “Caption” field.

The code

A new window (Visual Basic Editor) will open which will have a dialog box in the center. You will write the code in this dialog box.

Command Button VBA code

Code completed

This is how the complete code should look like

Sort Columns Code

After writing down the entire code close the window (Visual Basic Editor) by clicking on the cross(x) icon on the top right corner of the screen.

See also  How to Enable the Developer Tab in Excel

The result

Click on the command button and see the result.

Sorting Columns Sheet VBA

You have now successfully sorted data and names using VBA.