Adding Clickable Sorting to Worksheet Lists : Range « Excel « VBA / Excel / Access / Word






Adding Clickable Sorting to Worksheet Lists

 
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 
    Dim mnDirection As Integer 
    Dim mnColumn As Integer 
    If Target.Column < 5 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 
        Dim rg As Range 
        Set rg = Me.Cells(1, 1).CurrentRegion 
        rg.Sort Key1:=rg.Cells(1, mnColumn), _ 
                 Order1:=mnDirection, _ 
                 Header:=xlYes 
    
        Set rg = Nothing 

    End If 
End Sub 

 








Related examples in the same category

1.Deletes a range and then fills the resulting gap by shifting the other cells to the left:
2.Select a range and activate another
3.Highlights selected range
4.Modify multiple cells at once using a range reference (like A1:A2)
5.To enter the name Florence into cell C10, you assign the name to the Value property of the Range object
6.If the Range object is not in the active worksheet in the active workbook
7.Assign the value in C1 in the active sheet to D10 in the sheet named Sales, in the active workbook
8.Assign the value of a property to a variable so it can be used in later code
9.Copies the contents of range A1:B3 to the clipboard:
10.Find in a range
11.Read a range from InputBox
12.Count the blank elements in a range
13.Sum the elements in a range
14.Show Edit Ranges
15.Expression refers to a cell one row below cell A1 and two columns to the right of cell A1: this refers to the cell commonly known as C2
16.Displays a message box that shows the value in cell A1 on Sheet1:
17.read the Value property only for a single- cell Range object: statement generates an error
18.Change the Value property for a range of any size: statement enters the number 123 into each cell in a range
19.Value is the default property for a Range object.
20.The Text property returns a string that represents the text as displayed in a cell: the formatted value. The Text property is read-only.
21.Count property returns the number of cells in a range (all cells, not just the nonblank cells). It's a read-only property.
22.The Column property returns the column number of a single-cell range;
23.Row property returns the row number of a single-cell range.
24.If the Range object consists of more than one cell, the Column property returns the column number of the first column in the range
25.If the Range object consists of more than one cell, the Row property returns the row number of the first row in the range.
26.Address, a read-only property, displays the cell address for a Range object in absolute notation (a dollar sign before the column letter and before the row number).
27.HasFormula property
28.Use object variables to represent the ranges
29.Building the Table
30.Returns the type of a range in an area