Double click on table header to sort

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
If Not Intersect(Target, Sheets("EMPLOYEE").Range("A1")) Is Nothing Then
    Cancel = True
    ActiveSheet.Unprotect Password:="testing"
    ActiveWorkbook.Worksheets("EMPLOYEE").ListObjects("EList").Sort.SortFields _
        .Clear
    ActiveWorkbook.Worksheets("EMPLOYEE").ListObjects("EList").Sort.SortFields _
        .Add Key:=Range("EList[[#All],[SEN'#]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("EMPLOYEE").ListObjects("EList").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
      ActiveSheet.Protect Password:="testing"
End If
the above code works perfect however would like to change line below to reference the table header instead of Range("A1"). this will be useful if I need to add a column In front of the column where I don't need to go change the range. the table Header is named "SEN#".

VBA Code:
If Not Intersect(Target, Sheets("EMPLOYEE LIST").Range("A1")) Is Nothing Then

any help is greatly appreciated.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try the following...

VBA Code:
If Not Intersect(Target, Range("EList[[#Headers],[SEN'#]]")) Is Nothing Then

However, your macro can be re-written as follows...

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    If Not Intersect(Target, Range("EList[[#Headers],[SEN'#]]")) Is Nothing Then
        Cancel = True
        Me.Unprotect Password:="testing"
        With Me.ListObjects("EList").Sort
            With .SortFields
                .Clear
                .Add Key:=Range("EList[[#All],[SEN'#]]"), _
                    SortOn:=xlSortOnValues, _
                    Order:=xlAscending, _
                    DataOption:=xlSortNormal
            End With
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Me.Protect Password:="testing"
    End If

End Sub

Hope this helps!
 
Upvote 0
Solution
That's great, I'm glad I could help.

And thanks for your feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,128
Members
449,097
Latest member
mlckr

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