how to get a code for sort using column heading

kalyan kumar

New Member
Joined
Apr 9, 2011
Messages
2
Hi All,

I have date in column A7 to column G7. I want to sort column A using column D's title and not the cells number. Can you please provide me the VBA Code for it?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I don't understand what you mean by "sort column A using column D's title and not the cells number"
could you post a small before and after example. You could use Excel Jeanie to post your example
 
Upvote 0
Try This


ThisWorkBook
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Select Case Target.Row
       Case 7
 
            If IsEmpty(Target.Value) Then Exit Sub
            If Target.Column> [COLOR=red]You decide[/COLOR] then exit sub
            Static MySortType As Integer
            If MySortType = 0 Then
                MySortType = xlAscending
            ElseIf MySortType = xlAscending Then
                MySortType = xlDescending
            ElseIf MySortType = xlDescending Then
                MySortType = xlAscending
            End If
            Target.CurrentRegion.Offset(1).Sort key1:=Target, order1:=MySortType, Header:=xlYes
    End Select
End sub
 
Last edited:
Upvote 0
You have to do the offsets on the columns - that part I dont understand what you want to do.
 
Upvote 0
Please advice: Below is the the data i have. I have to sort Column A(that is Vicky column in this case) in ascending order only if, Column C's tittle is Rick. quick response is greatly appreciative as I am in urgent need of this.

<table width="256" border="0" cellpadding="0" cellspacing="0"><col style="width: 48pt;" width="64" span="4"> <tbody><tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; width: 48pt;" width="64" height="20">Vicky</td> <td class="xl65" style="border-left: medium none; width: 48pt;" width="64">Bhusal</td> <td class="xl65" style="border-left: medium none; width: 48pt;" width="64">Rick</td> <td class="xl65" style="border-left: medium none; width: 48pt;" width="64">Mask</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; border-top: medium none;" align="right" height="20">56</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">6</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">6</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">6</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; border-top: medium none;" align="right" height="20">42</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">12</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">12</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">12</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; border-top: medium none;" align="right" height="20">108</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">18</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">18</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">18</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; border-top: medium none;" align="right" height="20">205</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">24</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">24</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">24</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; border-top: medium none;" align="right" height="20">655</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">30</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">30</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">30</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; border-top: medium none;" align="right" height="20">1050</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">36</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">36</td> <td class="xl65" style="border-top: medium none; border-left: medium none;" align="right">36</td> </tr> </tbody></table>
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,391
Members
452,909
Latest member
VickiS

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