|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
方法1:
- Sub SortestI()
- Dim r%, i%, m
- Dim Arr(), Brr
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- With Worksheets("Sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- m = 0
- For i = 2 To r
- If .Cells(i, 1).MergeArea.Cells(1, 1).Address = .Cells(i, 1).Address Then
- m = m + 1
- ReDim Preserve Arr(1 To 3, 1 To m)
- Arr(1, m) = i
- Arr(2, m) = .Cells(i, 1).MergeArea.Rows.Count
- Arr(3, m) = .Cells(i, 2).Value
- End If
- Next
- For i = 1 To UBound(Arr, 2) - 1
- p = i
- For j = i + 1 To UBound(Arr, 2)
- If Arr(3, p) < Arr(3, j) Then
- p = j
- End If
- Next
- If p <> i Then
- For K = 1 To UBound(Arr)
- temp = Arr(K, i)
- Arr(K, i) = Arr(K, p)
- Arr(K, p) = temp
- Next
- End If
- Next
- i1 = r + 1
- For K = 1 To UBound(Arr, 2)
- .Cells(Arr(1, K), 1).Resize(Arr(2, K), 2).Copy .Cells(i1, 1)
- i1 = i1 + Arr(2, K)
- Next
- .Range("A2:B" & r).Delete shift:=xlUp
- End With
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "OK!"
- End Sub
复制代码
方法2:
- Sub SortestII()
- Dim r%, i%, m
- Dim Arr(), Brr
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- With Worksheets("Sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- m = 0
- For i = 2 To r
- If .Cells(i, 1).MergeArea.Cells(1, 1).Address = .Cells(i, 1).Address Then
- m = m + 1
- ReDim Preserve Arr(1 To 2, 1 To m)
- Set Arr(1, m) = .Cells(i, 1).Resize(.Cells(i, 1).MergeArea.Rows.Count, 2)
- Arr(2, m) = .Cells(i, 2).Value
- End If
- Next
- For i = 1 To UBound(Arr, 2) - 1
- p = i
- For j = i + 1 To UBound(Arr, 2)
- If Arr(2, p) < Arr(2, j) Then
- p = j
- End If
- Next
- If p <> i Then
- For K = 1 To UBound(Arr)
- If K = 1 Then
- Set temp = Arr(K, i)
- Set Arr(K, i) = Arr(K, p)
- Set Arr(K, p) = temp
- Else
- temp = Arr(K, i)
- Arr(K, i) = Arr(K, p)
- Arr(K, p) = temp
- End If
- Next
- End If
- Next
- i1 = r + 1
- For K = 1 To UBound(Arr, 2)
- Arr(1, K).Copy .Cells(i1, 1)
- i1 = i1 + Arr(1, K).Rows.Count
- Next
- .Range("A2:B" & r).Delete shift:=xlUp
- End With
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "OK!"
- End Sub
复制代码
|
|