通过下面的VBA代码,放入工作表的双击事件中,就可以得到双击工作表的列标题(双击列的名称),当前列会自动排序。
比如,将代码放在如下的事件中:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
代码如下:
Dim YouRg As Range
If Target.Column <= Me.Cells (1,1).CurrentRegion.Columns.Count And Target.Row = 1 Then
If Target.Column <> mnColumn Then
mnColumn = Target.Column
mnDirection = xlAscending
Else
If mnDirection = xlAscending Then
mnDirection = xlDescending
Else
mnDirection = xlAscending
End If
End If
Set YouRg = Me.Cells(1, 1).CurrentRegion
YouRg .Sort key1:=YouRg .Cells(1, mnColumn), order1:=mnDirection,header:=xlYes
Set YouRg = Nothing
Cancel = True
End If
以上代码是升序排序,你可以改为降序排序。