I have a sheet where i have headers for columns in Row 4. I had a code that when i double clicked on a cell in row 4 it sorted the data by that cell. The problem i have now is that in Column B, the cells are merged with the row below. So, for example row 4 and 5 are merged, row 6 and 7 etc. The code i have will no longer let me sort, due to these merged cells. Can anyone help?
Here is the code that i was using
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
lr = Cells(Rows.Count, "B").End(xlUp).row
lc = Cells(4, Columns.Count).End(xlToLeft).Column
If Target.row = 4 And Target.Column <= lc Then Range(Cells(4, "B"), Cells(lr, lc)).Sort Key1:=Cells(4, Target.Column), Header:=xlYes 'Order1:=xlDescending
End Sub
try
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lc As Integer
lc = Cells(4, Columns.Count).End(xlToLeft).Column
If Target.Row = 4 And Target.Column <= lc Then
sortdescent Target.Column - 1, lc
End If
End Sub
module code
Sub sortdescent(x As Integer, col As Integer)
Dim vDB
Dim strTemp()
Dim r As Integer, c As Integer, i As Integer, j As Integer
Dim m As Integer
ReDim strTemp(1 To 2, 1 To col)
vDB = Range("b5", Cells(Range("c" & Rows.Count).End(xlUp).Row, col))
r = UBound(vDB, 1)
c = UBound(vDB, 2)
For i = 1 To r Step 2
For j = 1 To r Step 2
'If vDB(j, x) > vDB(i, x) Then 'Ascent
If vDB(j, x) < vDB(i, x) Then 'Descent
For m = 1 To c
strTemp(1, m) = vDB(i, m)
strTemp(2, m) = vDB(i + 1, m)
vDB(i, m) = vDB(j, m)
vDB(i + 1, m) = vDB(j + 1, m)
vDB(j, m) = strTemp(1, m)
vDB(j + 1, m) = strTemp(2, m)
Next
End If
Next j
Next i
Range("b5").Resize(r, c) = vDB
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.