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
我在网上获得了上述代码,并根据我的要求进行了更改。它的工作正常,但它仅适用于当前工作表,我希望它可以在多个工作表上工作。运行后,所有数据都会被选中。运行数据后不应该选择。需要专家帮助。
答案 0 :(得分:0)
您必须实际遍历每个工作表并对该工作表进行操作。在编写代码时,没有规范可以遍历工作表或处理任何特定工作表。 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.