Hello BG1983,
If I understood you needs correctly then the attached workbook should do what you want.
Here is the code that was added...
Sheet1 Selection Change Event Code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, ActiveSheet.ListObjects(1).HeaderRowRange.Offset(-1, 0)) Is Nothing Then
Call SortColumn
End If
End Sub
Module1 Code
Sub SortColumn()
Dim Cell As Range
Dim Rng As Range
Dim sortOrder As Long
Dim Tbl As ListObject
On Error Resume Next
Set Tbl = ActiveSheet.ListObjects(1)
If Err <> 0 Then
MsgBox "There is no Table on this worksheet.", vbExclamation
Exit Sub
End If
On Error GoTo 0
Set Cell = Tbl.Range.Columns(ActiveCell.Column - Tbl.Range.Column + 1).Cells(1, 1)
With Tbl.Sort
If .SortFields.Count > 0 Then
sortOrder = .SortFields(1).Order
If sortOrder = 1 Then
sortOrder = 2
Else
sortOrder = 1
End If
Else
sortOrder = xlAscending
End If
.SortFields.Clear
.SortFields.Add Key:=Cell, Order:=sortOrder, SortOn:=xlSortOnValues, DataOption:=xlSortNormal
.Header = Yes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
End Sub
Bookmarks