【发布时间】:2020-02-17 22:36:17
【问题描述】:
又是我。我一直在尝试不同的替代方法来根据每个集合的 Col D 对行进行排序。
这是最接近的一个,但发现了 2 个错误。
1- 循环并且在到达最后使用的行时无法退出。 它一直在排序,直到我按下强制退出 2- 在每个集合只有一个 SKU 的情况下无法排序 它也对下一个集合进行排序。有时会排序 3 个集合。 例如运行前 - 第 9、29、32、35、45 行....
这是我的代码。我的代码有什么问题?
Sub SortingCollectionOnColD
With ActiveSheet.Range("A:A")
Set FindSubtotal = .Find("Subtotal", After:=.Range("A1"), LookIn:=xlValues)
If Not FindSubtotal Is Nothing Then
firstOne = FindSubtotal.Address
Do
With FindSubtotal
Range("A" & FindSubtotal.Row - 1).Select
Set SortRange = Range(Selection, Selection.End(xlUp)).EntireRow
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("C" & FindSubtotal.Row) _
, SortOn:=xlSortOnValues, Order:=xlAscending
With ActiveSheet.Sort
.SetRange SortRange
.Header = xlNo
.Orientation = xlTopToBottom
.Apply
End With
End With
Set FindSubtotal = .FindNext(FindSubtotal)
Loop While Not FindSubtotal Is Nothing And FindSubtotal.Address <> firstOne
End If
End With End Sub
赛前
预期结果
跑完之后。突出主要失败
【问题讨论】:
-
您的
With FindSubtotal... End With` 没有任何作用。我尝试了您的代码,但正确声明了所有变量并使用工作表而不是 A:A 范围,这对我来说看起来很奇怪,它按预期工作(由我......)。我不能把它贴在这里,因为你什么都不会明白。我会尝试一个答案。请对其进行测试,并让我知道它是否按预期工作......