Question : Are there Excel events? (Like in VBA)

Is there a way that clicking on an Excel cell can trigger an event like sorting a column (without using a VBA control), just like the button on the Excel toolbar? If there is, can you briefly explain how it works?

Dave G

Answer : Are there Excel events? (Like in VBA)

dgottardi,
The following sub will automatically sort a column when you doubleclick in row 1. It will switch from ascending to descending (and back) in alternating fashion with each successive doubleclick.

Install the sub in the code pane of the worksheet. To access this code pane, right click the sheet tab, then select View Code from the pop-up. Paste the code there, then ALT + F11 to return to the worksheet.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Static SortAscending(256) As Boolean
Dim i As Long
Dim rg As Range
If Intersect(Target, Rows(1)) Is Nothing Then Exit Sub
i = Target.Column
Set rg = Intersect(Columns(i), ActiveSheet.UsedRange)
If SortAscending(Target.Column) = False Then
    SortAscending(Target.Column) = True
    rg.Sort key1:=rg.Cells(1), order1:=xlAscending, header:=xlNo
Else
    SortAscending(Target.Column) = False
    rg.Sort key1:=rg.Cells(1), order1:=xlDescending, header:=xlNo
End If
End Sub

Brad
Random Solutions  
 
programming4us programming4us