想让每托的数据单独排序,每托的数据是空行隔开的优先按照e列排
excel吧
全部回复
仅看楼主
level 9
747519200 楼主
想让每托的数据单独排序,每托的数据是空行隔开的优先按照e列排序,其次按照d列排序。我都是手动一托托的拍太慢了,有没有别的办法
2021年06月09日 09点06分 1
level 11
托号按段排序吗
2021年06月09日 10点06分 2
level 11
Option Explicit
Sub 分段排序()
 Dim a, i, j, m, n, p
 a = Range("a2:h" & Cells(Rows.Count, "e").End(xlUp).Row + 1).Value
 For i = 1 To UBound(a) - 1
  For j = i To UBound(a) - 1
   If Len(a(j, 4)) Then m = i: p = i: Exit For
  Next
  For j = j To UBound(a)
   If Len(a(j, 4)) = 0 Then n = j - 1: Exit For
  Next
  Call bsort(a, m, n, 2, UBound(a, 2), 5)
  For j = m To n
   If a(j, 5) <> a(j + 1, 5) Then
    Call bsort(a, p, j, 2, UBound(a, 2), 4)
    p = j + 1
   End If
  Next
  i = n + 1
  DoEvents
 Next
 [a2].Offset(, UBound(a, 2) + 1).Resize(UBound(a) - 1, UBound(a, 2)) = a
End Sub
Function bsort(arr, first, last, left, right, key)
 Dim i, j, k, t
 For i = first To last - 1
  For j = first To last + first - 1 - i
   If arr(j, key) > arr(j + 1, key) Then
    For k = left To right
     t = arr(j, k): arr(j, k) = arr(j + 1, k): arr(j + 1, k) = t
    Next
   End If
  Next
 Next
End Function
2021年06月09日 10点06分 3
想问下,我一个工作表里有好几个工作簿,这个是排第一个工作簿吗
2021年06月09日 14点06分
@747519200 一个工作簿可以有多个工作表,你搞混了。当前工作表有效,也可以上个附件到百度网盘,把链接贴上来直接给你处理掉。
2021年06月09日 14点06分
@🌴菠萝蜜🌴 链接: https://pan.baidu.com/s/1LDSNWcSqlaa9aQYCzrVk7A 提取码: 4hn4 复制这段内容后打开百度网盘手机App,操作更方便哦
2021年06月10日 05点06分
@🌴菠萝蜜🌴 这里有几个工作表,每个工作表都要排序,
2021年06月10日 05点06分
level 13
sortby函数非常适合
2021年06月10日 02点06分 4
level 8
试试辅助列排序
2021年06月10日 02点06分 5
level 11
回复 747519200 :这个排序后就把表里的公式都变成数值了,除了ab列,别的列都是公式,能不改变公式吗
查了一下用Formula的使用方法是可以获取公式的,自己测试一下应该差不多
Option Explicit
Sub 分段排序()
 Dim a(1), i, j, m, n, p, sht
 For Each sht In Sheets
  With sht
   If Len(.[b2].Value) Then
    a(0) = .Range("a2:h" & .Cells(Rows.Count, "b").End(xlUp).Row + 1).Value
    a(1) = .Range("a2:h" & .Cells(Rows.Count, "b").End(xlUp).Row + 1).Formula
    For i = 1 To UBound(a(0)) - 1
     For j = i To UBound(a(0)) - 1
      If Len(a(0)(j, 5)) Then m = i: p = i: Exit For
     Next
     For j = j To UBound(a(0))
      If Len(a(0)(j, 5)) = 0 Then n = j - 1: Exit For
     Next
     Call bsort(a, m, n, 2, UBound(a(0), 2), 5)
     For j = m To n
      If a(0)(j, 5) <> a(0)(j + 1, 5) Or j = n Then
       Call bsort(a, p, j, 2, UBound(a(0), 2), 4)
       p = j + 1
      End If
     Next
     i = n + 1
    Next
    .[a2].Resize(UBound(a(1)) - 1, UBound(a(1), 2)) = a(1) 'a(0)输出数值
   End If
  End With
 Next
End Sub
Function bsort(a, first, last, left, right, key)
 Dim i, j, k, t
 For i = first To last - 1
  For j = first To last + first - 1 - i
   If a(0)(j, key) > a(0)(j + 1, key) Then
    For k = left To right
     t = a(0)(j, k): a(0)(j, k) = a(0)(j + 1, k): a(0)(j + 1, k) = t
     t = a(1)(j, k): a(1)(j, k) = a(1)(j + 1, k): a(1)(j + 1, k) = t
    Next
   End If
  Next
 Next
End Function
2021年06月15日 03点06分 6
level 9
747519200 楼主
用你这个新代码点了后会弹二次这个插口
2021年06月17日 06点06分 7
1