能不能帮忙弄一个排序的宏
excel吧
全部回复
仅看楼主
level 9
747519200 楼主
我正常操作是选中要排序的区域,然后在像图二那样点排序,有很多,能不能弄一个宏,我选择要排序的区域后在运行宏就可以。
2022年12月12日 10点12分 1
level 9
747519200 楼主
排序优先级是CDFE
2022年12月12日 10点12分 2
level 11
'可以指定列次序排序,列数可以不限
Option Explicit
Sub abc()
 Dim a, pos, i, j, k, p
 a = [a1].CurrentRegion.Offset(1).Value
 pos = Array(3, 4, 6, 5) 'C、D、F、E 列次序升序
 Call bsort(a, 1, UBound(a, 1) - 1, 1, UBound(a, 2), pos(0))
 For i = 1 To UBound(pos)
  p = 0
  For j = 1 To UBound(a, 1) - 1
   For k = i - 1 To 0 Step -1
    If a(j, pos(k)) <> a(j + 1, pos(k)) Then
     Call bsort(a, p + 1, j, 1, UBound(a, 2), pos(i))
     p = j: Exit For
    End If
   Next
  Next
 Next
 [a1].Offset(1, UBound(a, 2) + 1).Resize(UBound(a, 1) - 1, UBound(a, 2)) = a
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(j, key) > a(j + 1, key) Then
    For k = left To right
     t = a(j, k): a(j, k) = a(j + 1, k): a(j + 1, k) = t
    Next
   End If
  Next
 Next
End Function
2022年12月12日 11点12分 4
和我要的结果不一样,我是要把我选中的区域排序,不是把排序的结果复制到旁边。我后面的数据都是依据b列的数量计算来的,都是公式
2022年12月12日 12点12分
给大佬点赞!
2022年12月12日 14点12分
不过这个问题还是用vba自带的sort就行。
2022年12月12日 14点12分
@数界如此多娇 感谢感谢 [哈哈]
2022年12月12日 14点12分
level 9
录制宏
2022年12月12日 21点12分 6
level 5
软件的基础功能不就是你的需求解决办法吗?为啥要另外弄个,似乎多此一举啊
2022年12月15日 09点12分 7
因为要排的很多,一点点的点击慢
2022年12月17日 02点12分
level 11
'限定6列数据排序,纯数据不带标题
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Target.Rows.Count < 2 Or Target.Columns.Count <> 6 Then Exit Sub
 If Target.Rows.Count = Rows.Count Then Exit Sub
 Dim x, y, a, i, j, k, p, pos
 x = Target.Row: y = Target.Column
 a = Cells(x, y).Resize(Target.Rows.Count + 1, 6).Value
 pos = Array(3, 4, 6, 5) '后4列
 Call qsort(a, 1, UBound(a, 1) - 1, 1, 6, pos(0))
 For i = 1 To UBound(pos)
  p = 0
  For j = 1 To UBound(a, 1) - 1
   For k = i - 1 To 0 Step -1
    If a(j, pos(k)) <> a(j + 1, pos(k)) Then
     If j - p > 2 Then Call qsort(a, p + 1, j, 1, 6, pos(i))
     p = j: Exit For
    End If
   Next
  Next
 Next
 Application.EnableEvents = False
 Cells(x, y).Resize(UBound(a, 1) - 1, 6) = a
 Application.EnableEvents = True
End Sub
Function qsort(a, first, last, left, right, key)
 Dim i As Long, j As Long, k As Long, x, t
 i = first: j = last: x = a((first + last) \ 2, key)
 While i <= j
  While a(i, key) < x: i = i + 1: Wend
  While x < a(j, key): j = j - 1: Wend
  If i <= j Then
   For k = left To right
    t = a(i, k): a(i, k) = a(j, k): a(j, k) = t
   Next
   i = i + 1: j = j - 1
  End If
 Wend
 If first < j Then qsort a, first, j, left, right, key
 If i < last Then qsort a, i, last, left, right, key
End Function
2022年12月18日 08点12分 9
无效内部过程,能帮我看看吗,图片在10楼
2022年12月24日 08点12分
level 9
747519200 楼主
2022年12月24日 08点12分 10
第一行删除。另外这是一个change事件并不是一个简单的过程。自己查一下如何在Excel中使用change事件就知道了。
2022年12月24日 10点12分
level 9
747519200 楼主
2022年12月24日 11点12分 11
@🌴菠萝蜜🌴 好像不行。我的数据是行行扫码输入的,输入完在排序
2022年12月24日 11点12分
@747519200 还是删除第一行。粘贴之前把当前页面中的内容先全部清除掉。
2022年12月24日 12点12分
@747519200 感觉有点累,太费劲了,还是去玩我的游戏吧。在这个帖子中我不会再作回复了。
2022年12月24日 13点12分
@🌴菠萝蜜🌴 是我太笨了[泪],不过还是谢谢你。
2022年12月24日 13点12分
level 13
Sub 这个能排3个和3个以下key()
With Selection
.Sort .Cells(1, 1), 1, .Cells(1, 2), , 1, .Cells(1, 3), 1, 0
End With
End Sub
=================================================
Sub 超过3个key用这个()
Selection.Offset(-1).Resize(1 + Selection.Rows.Count).AutoFilter
With ActiveSheet.AutoFilter.Sort
With .SortFields
.Clear
.Add2 Key:=Selection.Columns(1)
.Add2 Key:=Selection.Columns(2)
.Add2 Key:=Selection.Columns(3)
.Add2 Key:=Selection.Columns(4)
End With
.Apply
End With
Selection.AutoFilter
End Sub
2022年12月24日 14点12分 13
好像不行,图片在落下
2022年12月25日 07点12分
level 9
747519200 楼主
2022年12月25日 07点12分 14
1