Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2487|回复: 8

[已解决]请教vba排序

[复制链接]
发表于 2015-7-24 17:59 | 显示全部楼层 |阅读模式
本帖最后由 武林长风 于 2015-7-24 20:47 编辑

点击按钮,按各科的及格率(优秀率)将各校由高到低排序。
最佳答案
2015-7-24 19:11
本帖最后由 gufengaoyue 于 2015-7-24 19:22 编辑

哦,没注意看到是2个表,改下就好了。

  1. Sub 排序()
  2. Dim arr, i&, Sht As Worksheet
  3. For Each Sht In Sheets
  4.     Sht.Activate
  5.         For i = 1 To Cells(2, "iv").End(xlToLeft).Column Step 2
  6.             arr = Range(Cells(3, i), Cells(Rows.Count, i + 1).End(3))
  7.             BubbleSort2 arr
  8.             Range(Cells(3, i), Cells(Rows.Count, i + 1).End(3)) = arr
  9.         Next
  10. Next
  11. End Sub
  12. Sub BubbleSort2(ByRef arr)
  13.   Dim i&, j&, vSwap1, vSwap2
  14.   For i = UBound(arr) To 2 Step -1
  15.     For j = 1 To i - 1
  16.       If arr(j, 2) < arr(j + 1, 2) Then
  17.         vSwap1 = arr(j, 1)
  18.         vSwap2 = arr(j, 2)
  19.         arr(j, 1) = arr(j + 1, 1)
  20.         arr(j, 2) = arr(j + 1, 2)
  21.         arr(j + 1, 1) = vSwap1
  22.         arr(j + 1, 2) = vSwap2
  23.       End If
  24.     Next
  25.   Next
  26. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2015-7-24 18:18 | 显示全部楼层
  1. Sub XX()
  2. For i = 1 To Cells(2, "iv").End(xlToLeft).Column Step 2
  3.     ActiveSheet.Sort.SortFields.Clear
  4.     Range(Cells(2, i), Cells(Rows.Count, i + 1).End(3)).Select
  5.     Range("A1").AutoFilter Field:=1
  6.     Range("A1").AutoFilter
  7.     Selection.AutoFilter
  8.     ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Cells(2, i + 1), SortOn:=xlSortOnValues, Order:=xlDescending
  9.     ActiveSheet.AutoFilter.Sort.Apply
  10. Next
  11. Cells.AutoFilter
  12. End Sub
复制代码

点评

变量I未定义  发表于 2015-7-24 18:37
回复

使用道具 举报

发表于 2015-7-24 18:49 | 显示全部楼层
好吧。
  1. Sub XX()
  2. dim i&
  3. For i = 1 To Cells(2, "iv").End(xlToLeft).Column Step 2
  4.     ActiveSheet.Sort.SortFields.Clear
  5.     Range(Cells(2, i), Cells(Rows.Count, i + 1).End(3)).Select
  6.     Range("A1").AutoFilter Field:=1
  7.     Range("A1").AutoFilter
  8.     Selection.AutoFilter
  9.     ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Cells(2, i + 1), SortOn:=xlSortOnValues, Order:=xlDescending
  10.     ActiveSheet.AutoFilter.Sort.Apply
  11. Next
  12. Cells.AutoFilter
  13. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2015-7-24 18:51 | 显示全部楼层
本帖最后由 武林长风 于 2015-7-24 18:52 编辑
gufengaoyue 发表于 2015-7-24 18:49
好吧。

360截图20150724184627437.jpg
ActiveSheet.Sort.SortFields.Clear
回复

使用道具 举报

发表于 2015-7-24 18:59 | 显示全部楼层
本帖最后由 gufengaoyue 于 2015-7-24 19:00 编辑
武林长风 发表于 2015-7-24 18:51
ActiveSheet.Sort.SortFields.Clear

把这句删了。你用的2003版的?
我没有2003版的可以测。
回复

使用道具 举报

 楼主| 发表于 2015-7-24 19:03 | 显示全部楼层
gufengaoyue 发表于 2015-7-24 18:59
把这句删了。你用的2003版的?
我没有2003版的可以测。

删后这句出错了:
1.jpg
回复

使用道具 举报

发表于 2015-7-24 19:11 | 显示全部楼层    本楼为最佳答案   
本帖最后由 gufengaoyue 于 2015-7-24 19:22 编辑

哦,没注意看到是2个表,改下就好了。

  1. Sub 排序()
  2. Dim arr, i&, Sht As Worksheet
  3. For Each Sht In Sheets
  4.     Sht.Activate
  5.         For i = 1 To Cells(2, "iv").End(xlToLeft).Column Step 2
  6.             arr = Range(Cells(3, i), Cells(Rows.Count, i + 1).End(3))
  7.             BubbleSort2 arr
  8.             Range(Cells(3, i), Cells(Rows.Count, i + 1).End(3)) = arr
  9.         Next
  10. Next
  11. End Sub
  12. Sub BubbleSort2(ByRef arr)
  13.   Dim i&, j&, vSwap1, vSwap2
  14.   For i = UBound(arr) To 2 Step -1
  15.     For j = 1 To i - 1
  16.       If arr(j, 2) < arr(j + 1, 2) Then
  17.         vSwap1 = arr(j, 1)
  18.         vSwap2 = arr(j, 2)
  19.         arr(j, 1) = arr(j + 1, 1)
  20.         arr(j, 2) = arr(j + 1, 2)
  21.         arr(j + 1, 1) = vSwap1
  22.         arr(j + 1, 2) = vSwap2
  23.       End If
  24.     Next
  25.   Next
  26. End Sub
复制代码

评分

参与人数 1 +5 收起 理由
武林长风 + 5 赞一个!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2015-7-24 19:18 | 显示全部楼层
gufengaoyue 发表于 2015-7-24 19:11
那用这个吧

要一个按钮同时完成2个工作表。
回复

使用道具 举报

发表于 2015-7-24 19:23 来自手机 | 显示全部楼层
7楼已改了…来自: iPhone客户端
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-4 22:52 , Processed in 0.161789 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表