Sub Descending_Click()
Dim j As Integer, k As Integer
j = Worksheets.Count
For k = 1 To j
Selection.Sort key1:=Range("L2"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next k
End Sub
我在网上获得了上述代码,并根据需要进行了更改。它的工作正常,但仅在当前工作表上工作,我希望它在多张工作表上工作。同样在运行之后,所有数据都将被选中。运行后不应选择数据。需要专家帮助。
您实际上必须遍历每个工作表并对该表进行操作。在编写代码时,没有规范可以遍历工作表或在任何特定工作表上工作。该Selection
语句仅对当时选择的活动起作用。
Sub Descending_Click()
Dim ws as Worksheet
For Each ws in Worksheets
ws.UsedRange.Sort key1:=Range("L2"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next ws
End Sub
'Note: you made need to specify the exact range on the worksheet, depending on how your data is set up. In that case, replace UsedRange with Range("A1:K100") or whatever your range actually is.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句