vba 一次对工作簿中的所有工作表进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17748350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:20:02  来源:igfitidea点击:

Sorting all worksheets in a workbook at once

sortingexcel-vbavbaexcel

提问by Andy Ardueser

I have a workbook with 4 worksheets (Sheet1, Sheet2, ...). The sheets are the same format; the columns are identical variables, but they have different values below. So in theory, they should be able to be sorted in the same way. I've tried to ctrl-select all the tabs, but the Sort option becomes unavailable at that point.

我有一个包含 4 个工作表(Sheet1、Sheet2、...)的工作簿。表格格式相同;列是相同的变量,但它们在下面具有不同的值。所以理论上,它们应该能够以相同的方式进行排序。我尝试按 ctrl 选择所有选项卡,但此时排序选项不可用。

Does anyone know if there's a way to do this without making a different macro for each of the 4 sheets? That seems annoyingly redundant. Any help is appreciated.

有谁知道是否有办法做到这一点,而无需为 4 张纸中的每一张都制作不同的宏?这看起来很烦人的多余。任何帮助表示赞赏。

回答by Derek Cheng

I assume you are referring to the menu function sort. It doesn't allow user to select cells in different worksheets and sort them. You could implement a For each loop to sort data in each worksheet within the same macro. Something like the following

我假设您指的是菜单功能排序。它不允许用户选择不同工作表中的单元格并对它们进行排序。您可以实现 For each 循环来对同一宏中每个工作表中的数据进行排序。类似于以下内容

Sub SortingAllWorksheet()
Dim wsh As Worksheet
For Each wsh In ThisWorkbook.Sheets
    'sort columns A to C based on data in column C
    wsh.Columns("A:C").Sort key1:=Range("C2"), order1:=xlAscending, Header:=xlYes
Next
End Sub