简体   繁体   English

在多张纸中按降序对数据进行排序

[英]sort data in descending order in multiple sheets

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

I got above code on the web and made changes as per my requirement. 我在网上获得了上述代码,并根据需要进行了更改。 Its working fine but it works on current sheet only, I want to it to work on multiple sheets. 它的工作正常,但仅在当前工作表上工作,我希望它在多张工作表上工作。 Also after running, all data get selected. 同样在运行之后,所有数据都将被选中。 After running data should not be select. 运行后不应选择数据。 Need expert help. 需要专家帮助。

You have to actually loop through each worksheet and act on that sheet. 您实际上必须遍历每个工作表并对该表进行操作。 As your code is written, there is no specification to loop through the sheets or to work on any specific sheet. 在编写代码时,没有规范可以遍历工作表或在任何特定工作表上工作。 The Selection statement just works on what is active selected at that moment. 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM