|
楼主 |
发表于 2022-10-12 15:48
|
显示全部楼层
本帖最后由 ning84 于 2022-10-12 17:46 编辑
菜鸟学习体会,数组+字典等方法很难实现,不可能比Excel内置的排序功能效率高。
- Sub ls()
- Dim Sht As Worksheet
- Set Sht = Sheet2
- Dim Rng As Range, oRng As Range
- Set Rng = Cells(4, 1).CurrentRegion
- 'Debug.Print Rng.Address
- ''
- For ii = 1 To Rng.Rows.Count
- s = Split(Rng(ii, 1), ".")
- Rng(ii, 4) = Rng(ii, 1)
- Rng(ii, 5) = Rng(ii, 2)
- For jj = 0 To UBound(s)
- Rng(ii, 6 + jj) = s(jj)
- Next jj
- Next ii
-
- Set Rng = Rng(ii - 1, jj).CurrentRegion
- With Sht.Sort
- With .SortFields
- .Clear
- For jj = 3 To Rng.Columns.Count
- Set oRng = Rng(1, jj).Resize(Rng.Rows.Count, 1)
- 'Debug.Print oRng.Address(RowAbsolute:=False, ColumnAbsolute:=False)
- Debug.Print oRng.Address(0, 0)
- .Add Key:=Range(oRng.Address(0, 0)) _
- , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
- Next jj
- End With
- .SetRange Rng
- .Header = xlGuess
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
-
- End With
- ''
- End Sub
复制代码- Sub ll()
- Dim Rng As Range, SortRng As Range
- With Sheet2
- Set Rng = .Cells(4, 1).CurrentRegion
- Set SortRng = .Cells(4, 1).Resize(Rng.Rows.Count, 1)
- Debug.Print Rng.Address, SortRng.Address
- SpecialCharacterSeparation Rng, SortRng, ".", 8
- End With
- End Sub
- Function SpecialCharacterSeparation(ManyRng As Range, SortRng As Range, Separation As String, Col As Integer)
- Dim Sht As Worksheet
- Set Sht = ManyRng.Parent
- Dim Rng As Range, tmpSortRng As Range
- ''
- For ii = 1 To ManyRng.Rows.Count
- s = Split(SortRng(ii, 1), Separation)
- For jj = 0 To UBound(s)
- Sht.Cells(ManyRng.Row + ii - 1, Col + jj) = s(jj)
- Next jj
- Next ii
- ''
- Set Rng = Sht.Cells(ManyRng.Row + ii - 1 - 1, Col + jj - 1).CurrentRegion
- Set tmpSortRng = Rng
- Set Rng = Sht.Cells(ManyRng.Row, 1).Resize(Rng.Rows.Count, Rng.Column + Rng.Columns.Count - 1)
- Debug.Print Rng.Address
- Stop
- With Sht.Sort
- With .SortFields
- .Clear
- For jj = 1 To tmpSortRng.Columns.Count
- Set oRng = tmpSortRng(1, jj).Resize(Rng.Rows.Count, 1)
- .Add Key:=Range(oRng.Address(0, 0)) _
- , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
- Next jj
- End With
- .SetRange Rng
- .Header = xlGuess
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
-
- End Function
复制代码
|
|