Excel精英培训网

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

[已解决]求教对一个工作簿中的所有工作表都按照班级升序?

[复制链接]
发表于 2014-5-19 14:48 | 显示全部楼层 |阅读模式
本帖最后由 eikok 于 2014-5-19 15:42 编辑

求教对一个工作簿中的所有工作表都按照班级升序?
Sub 宏3()
    Columns("B:E").Select
    ActiveWorkbook.Worksheets("高2文").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("高2文").Sort.SortFields.Add Key:=Range("E2:E1229"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("高2文").Sort
        .SetRange Range("B1:E1229")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
E列是班级列
最佳答案
2014-5-19 15:33
  1. Sub 宏3()
  2.     Dim sht As Worksheet, lCol&
  3.     Application.ScreenUpdating = False
  4.     For Each sht In Worksheets
  5.         With sht
  6.             lCol = .Cells(Rows.Count, "e").End(xlUp).Row
  7.             With .Sort.SortFields
  8.                 .Clear
  9.                 .Add Key:=sht.Range("e2:e" & lCol), _
  10.                      SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  11.             End With
  12.             With .Sort
  13.                 .SetRange sht.Range("b1:e" & lCol)
  14.                 .Header = xlYes
  15.                 .MatchCase = False
  16.                 .Orientation = xlTopToBottom
  17.                 .SortMethod = xlPinYin
  18.                 .Apply
  19.             End With
  20.         End With
  21.     Next
  22.     Application.ScreenUpdating = True
  23.     MsgBox "排序完成"
  24. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-5-19 14:58 | 显示全部楼层
  1. Sub 宏3()
  2.     Dim sht As Worksheet
  3.     Dim lCol&
  4.     Application.ScreenUpdating = False
  5.    
  6.     For Each sht In Worksheets
  7.         With sht
  8.             lCol = .Cells(Rows.Count, "e").End(xlUp).Row
  9.             With .Sort.SortFields
  10.                 .Clear
  11.                 .Add Key:=.Cells("e2:e" & lCol), _
  12.                      SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  13.             End With
  14.             With .Sort
  15.                 .SetRange .Cells("b1:e" & lCol)
  16.                 .Header = xlYes
  17.                 .MatchCase = False
  18.                 .Orientation = xlTopToBottom
  19.                 .SortMethod = xlPinYin
  20.                 .Apply
  21.             End With
  22.         End With
  23.     Next
  24.     Application.ScreenUpdating = True
  25.     MsgBox "排序完成"
  26. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-5-19 15:03 | 显示全部楼层
本帖最后由 eikok 于 2014-5-19 15:06 编辑
hwc2ycy 发表于 2014-5-19 14:58


请问这个代码是在模块中运行吗?

1.png
2.png
回复

使用道具 举报

发表于 2014-5-19 15:13 | 显示全部楼层
哦,这里了,你在cells前加个sht
回复

使用道具 举报

发表于 2014-5-19 15:25 | 显示全部楼层
后面的还有要加的,要不你把附件传上来,我测测。
回复

使用道具 举报

发表于 2014-5-19 15:33 | 显示全部楼层
犯傻了,把cells写成了rang
回复

使用道具 举报

发表于 2014-5-19 15:33 | 显示全部楼层    本楼为最佳答案   
  1. Sub 宏3()
  2.     Dim sht As Worksheet, lCol&
  3.     Application.ScreenUpdating = False
  4.     For Each sht In Worksheets
  5.         With sht
  6.             lCol = .Cells(Rows.Count, "e").End(xlUp).Row
  7.             With .Sort.SortFields
  8.                 .Clear
  9.                 .Add Key:=sht.Range("e2:e" & lCol), _
  10.                      SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  11.             End With
  12.             With .Sort
  13.                 .SetRange sht.Range("b1:e" & lCol)
  14.                 .Header = xlYes
  15.                 .MatchCase = False
  16.                 .Orientation = xlTopToBottom
  17.                 .SortMethod = xlPinYin
  18.                 .Apply
  19.             End With
  20.         End With
  21.     Next
  22.     Application.ScreenUpdating = True
  23.     MsgBox "排序完成"
  24. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-5-19 15:34 | 显示全部楼层
本帖最后由 eikok 于 2014-5-19 15:36 编辑


版主辛苦

楼上的代码可用.rar

3.82 KB, 下载次数: 4

回复

使用道具 举报

发表于 2014-5-19 15:39 | 显示全部楼层
工作簿里啥数据也没有,咱测了,刚刚传的代码我在自己的电脑上已经测试过了。
回复

使用道具 举报

 楼主| 发表于 2014-5-19 15:42 | 显示全部楼层
hwc2ycy 发表于 2014-5-19 15:39
工作簿里啥数据也没有,咱测了,刚刚传的代码我在自己的电脑上已经测试过了。

版主辛苦了{:1112:}
8楼发错了
可以删除
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 05:18 , Processed in 0.378874 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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