Excel精英培训网

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

[已解决]对多个工作簿进行自定义排序,太多,太繁琐了,求个代码

[复制链接]
发表于 2022-8-3 08:51 | 显示全部楼层 |阅读模式
1学分
首先在E列添加公式(=IF(COUNTIF([标准件清单.xlsx]Sheet1!$A$1:$A$522,C3),"1","2")),对C列进行材料分类,后对E列跟C列进行自定义排序,E列优先,BCD3列对应的数据不能乱,一般都有150个左右的工作簿。
最佳答案
2022-8-3 08:51
Sub 排序()
    Dim fso As Object
    Dim Wb As Workbook
    Dim Sh As Worksheet
    Dim D As Object
    Dim R&, Ar, f, T
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    T = Timer
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set D = CreateObject("scripting.dictionary")
    With Sheet1
        Ar = .[A1].CurrentRegion
        For R = 1 To UBound(Ar)
            D(Ar(R, 1)) = ""
        Next R
    End With
    For Each f In fso.getfolder(ThisWorkbook.Path).Files
        If InStr(f.Name, ThisWorkbook.Name) = 0 Then
            With Workbooks.Open(f)
                For Each Sh In .Worksheets
                    If Application.CountA(Sh.Cells) > 0 Then
                        With Sh
                            R = .Cells(.Rows.Count, 3).End(xlUp).Row
                            Ar = .Cells(3, 1).Resize(R - 2, 5)
                            For R = 1 To UBound(Ar)
                                If D.exists(Ar(R, 3)) Then
                                    Ar(R, 5) = "1"
                                Else
                                    Ar(R, 5) = "2"
                                End If
                            Next R
                            .Cells(3, 1).Resize(UBound(Ar), 5) = Ar
                            .Sort.SortFields.Clear
                            .Sort.SortFields.Add Key:=.Range("E3:E" & UBound(Ar) + 2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                            .Sort.SortFields.Add Key:=.Range("C3:C" & UBound(Ar) + 2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                            With .Sort
                                .SetRange Range("A3:E" & UBound(Ar) + 2)
                                .Header = xlGuess
                                .MatchCase = False
                                .Orientation = xlTopToBottom
                                .SortMethod = xlPinYin
                                .Apply
                            End With
                            .Columns("E") = ""
                        End With
                    End If
                Next Sh
                .Save
                .Close False
            End With
        End If
    Next f
    MsgBox "用时:" & Format(Timer - T, "0.00\秒")
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
1659487631397.jpg

附件.zip

43.41 KB, 下载次数: 8

最佳答案

查看完整内容

Sub 排序() Dim fso As Object Dim Wb As Workbook Dim Sh As Worksheet Dim D As Object Dim R&, Ar, f, T Application.DisplayAlerts = False Application.ScreenUpdating = False T = Timer Set fso = CreateObject("Scripting.FileSystemObject") Set D = CreateObject("scripting.dictionary") With Sheet1 Ar = .[A1].CurrentRegion For R = 1 To ...
发表于 2022-8-3 08:51 | 显示全部楼层    本楼为最佳答案   
Sub 排序()
    Dim fso As Object
    Dim Wb As Workbook
    Dim Sh As Worksheet
    Dim D As Object
    Dim R&, Ar, f, T
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    T = Timer
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set D = CreateObject("scripting.dictionary")
    With Sheet1
        Ar = .[A1].CurrentRegion
        For R = 1 To UBound(Ar)
            D(Ar(R, 1)) = ""
        Next R
    End With
    For Each f In fso.getfolder(ThisWorkbook.Path).Files
        If InStr(f.Name, ThisWorkbook.Name) = 0 Then
            With Workbooks.Open(f)
                For Each Sh In .Worksheets
                    If Application.CountA(Sh.Cells) > 0 Then
                        With Sh
                            R = .Cells(.Rows.Count, 3).End(xlUp).Row
                            Ar = .Cells(3, 1).Resize(R - 2, 5)
                            For R = 1 To UBound(Ar)
                                If D.exists(Ar(R, 3)) Then
                                    Ar(R, 5) = "1"
                                Else
                                    Ar(R, 5) = "2"
                                End If
                            Next R
                            .Cells(3, 1).Resize(UBound(Ar), 5) = Ar
                            .Sort.SortFields.Clear
                            .Sort.SortFields.Add Key:=.Range("E3:E" & UBound(Ar) + 2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                            .Sort.SortFields.Add Key:=.Range("C3:C" & UBound(Ar) + 2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                            With .Sort
                                .SetRange Range("A3:E" & UBound(Ar) + 2)
                                .Header = xlGuess
                                .MatchCase = False
                                .Orientation = xlTopToBottom
                                .SortMethod = xlPinYin
                                .Apply
                            End With
                            .Columns("E") = ""
                        End With
                    End If
                Next Sh
                .Save
                .Close False
            End With
        End If
    Next f
    MsgBox "用时:" & Format(Timer - T, "0.00\秒")
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
回复

使用道具 举报

 楼主| 发表于 2022-8-3 09:37 | 显示全部楼层
回复

使用道具 举报

发表于 2022-8-3 10:49 | 显示全部楼层
本帖最后由 hasyh2008 于 2022-8-3 11:13 编辑

不知道你的自定义规则,就随便排了下序。

排序(20220803).rar

40.18 KB, 下载次数: 1

回复

使用道具 举报

 楼主| 发表于 2022-8-3 11:21 | 显示全部楼层
hasyh2008 发表于 2022-8-3 10:49
不知道你的自定义规则,就随便排了下序。

这样子不对,C列跟E列都是升序,E列优先,可以的话,执行完排序了可以直接把E列的数据删了
回复

使用道具 举报

发表于 2022-8-3 11:58 | 显示全部楼层
                .Cells(2, 1).Resize(UBound(Ar) + 1, 5).Sort key1:="是否标准件", order1:=xlAscending, Header:=xlYes
                .Cells(2, 1).Resize(UBound(Ar) + 1, 5).Sort key1:="模板编号", order1:=xlAscending, Header:=xlYes

如果不对的话,把这两句调换下位置。

排序(20220803)(1).rar

41.48 KB, 下载次数: 2

回复

使用道具 举报

 楼主| 发表于 2022-8-3 13:33 | 显示全部楼层
hasyh2008 发表于 2022-8-3 11:58
.Cells(2, 1).Resize(UBound(Ar) + 1, 5).Sort key1:="是否标准件", order1:=xlAscending, ...

不好意思啊,还是不能用,就是是否标准件那一个命令错误
回复

使用道具 举报

发表于 2022-8-3 13:42 | 显示全部楼层
把你的要求表述清楚
回复

使用道具 举报

 楼主| 发表于 2022-8-3 13:59 | 显示全部楼层
hasyh2008 发表于 2022-8-3 13:42
把你的要求表述清楚

会有很多个这样的工作簿,我按我平时的操作来说吧,在E列输入公式判断模板型号是否是标准件,是为1,不是为2,然后选择工程编码,模板编号,数量,是否标准件以下的所有内容,进行自定义排序,E列跟C列,他们是同时排序的,E列是主要关键词,C列是次要,全部操作好再删除E列内容。。要是简单的话再加个合并文件,多个工作簿放在一个文件里
回复

使用道具 举报

发表于 2022-8-3 14:11 | 显示全部楼层
自定义排序,就要有自定义排序的规则,这个规则你没说啊。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-30 11:07 , Processed in 0.677327 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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