Macro to sort a sheet automatically based on the values in a column?

mrmiyagi

Board Regular
Joined
Dec 15, 2009
Messages
127
Hello,

I have an excel sheet that I need auto sort in based on a column.

<table width="128" border="0" cellpadding="0" cellspacing="0"><col style="width: 48pt;" width="64" span="2"> <tbody><tr style="height: 15.75pt;" height="21"> <td class="xl63" style="height: 15.75pt; width: 48pt;" width="64" height="21">Name</td> <td class="xl63" style="border-left: medium none; width: 48pt;" width="64">Rank</td> </tr> <tr style="height: 15.75pt;" height="21"> <td class="xl63" style="height: 15.75pt; border-top: medium none;" height="21">Tony</td> <td class="xl63" style="border-top: medium none; border-left: medium none;" align="right">1</td> </tr> <tr style="height: 15.75pt;" height="21"> <td class="xl63" style="height: 15.75pt; border-top: medium none;" height="21">Joe</td> <td class="xl63" style="border-top: medium none; border-left: medium none;" align="right">2</td> </tr> <tr style="height: 15.75pt;" height="21"> <td class="xl63" style="height: 15.75pt; border-top: medium none;" height="21">Jimmy</td> <td class="xl63" style="border-top: medium none; border-left: medium none;" align="right">3</td> </tr> <tr style="height: 15.75pt;" height="21"> <td class="xl63" style="height: 15.75pt; border-top: medium none;" height="21">Frank</td> <td class="xl63" style="border-top: medium none; border-left: medium none;" align="right">4</td> </tr> <tr style="height: 15.75pt;" height="21"> <td class="xl63" style="height: 15.75pt; border-top: medium none;" height="21">John</td> <td class="xl63" style="border-top: medium none; border-left: medium none;" align="right">5</td> </tr> </tbody></table>
How can I achieve this with a macro?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Then
    Application.EnableEvents = False
    Columns("A:B").Sort Key1:=Range("B2"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,254
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top