Hi,
I have two tables of data in a wksht, both with row and column headers. The column headers for both tables are located in Row9 so they align with each other on the sheet and each table is separated with some blank columns. The code below allows me to sort each table by double clicking either a row or column header and it works well. However, I'd like to introduce a macro (Macro12) into the second table only that executes just before the rows or columns sort on the double click of a header. What I have below works for the second table only, with Macro12 executed, when I double click a row header, however when any column headers are clicked in either table (because both tables have headers in Row9) macro 12 is executed - so by clicking a column header in the first table it executes macro 12 in the second table - how can I change the code so macro 12 only executes when column headers in the second table are clicked??
Any help much appreciated.
I have two tables of data in a wksht, both with row and column headers. The column headers for both tables are located in Row9 so they align with each other on the sheet and each table is separated with some blank columns. The code below allows me to sort each table by double clicking either a row or column header and it works well. However, I'd like to introduce a macro (Macro12) into the second table only that executes just before the rows or columns sort on the double click of a header. What I have below works for the second table only, with Macro12 executed, when I double click a row header, however when any column headers are clicked in either table (because both tables have headers in Row9) macro 12 is executed - so by clicking a column header in the first table it executes macro 12 in the second table - how can I change the code so macro 12 only executes when column headers in the second table are clicked??
Any help much appreciated.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rng As Range
Static sortAscending As Boolean
With Target
If (.Column = 5 And .Row = 9) Or (.Row <> 9 And .Column <> 5) Then
Rem do nothing
ElseIf .Row = 9 Then
.CurrentRegion.Sort Key1:=.Cells, Order1:=2 + CLng(sortAscending), Orientation:=xlSortColumns, Header:=True
sortAscending = Not sortAscending
Cancel = True
ElseIf .Column = 5 Then
Set rng = .CurrentRegion
.CurrentRegion.Offset(0, 1).Sort _
Key1:=.Cells, _
Order1:=2 + CLng(sortAscending), _
Orientation:=xlSortRows, _
Header:=True
sortAscending = Not sortAscending
Cancel = True
End If
End With
With Target
If (.Column = 19 And .Row = 9) Or (.Row <> 9 And .Column <> 19) Then
Rem do nothing
ElseIf .Row = 9 Then
[B]Call Macro12[/B]
.CurrentRegion.Sort Key1:=.Cells, Order1:=2 + CLng(sortAscending), Orientation:=xlSortColumns, Header:=True
sortAscending = Not sortAscending
Cancel = True
ElseIf .Column = 19 Then
[B] Call Macro12[/B]
Set rng = .CurrentRegion
.CurrentRegion.Offset(0, 1).Sort _
Key1:=.Cells, _
Order1:=2 + CLng(sortAscending), _
Orientation:=xlSortRows, _
Header:=True
sortAscending = Not sortAscending
Cancel = True
End If
End With