vba excel多列独立排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17241302/
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
Independently sort multiple columns in excel
提问by Batman
I'm trying to sort a large number of lists from A to Z. But if I sort column A, I don't want all other columns to be rearranged. I need all these lists to indivually be sorts alphabetically. I know it's possible to do one by one but I have 278 columns. Is there a way do automate it?
我正在尝试从 A 到 Z 对大量列表进行排序。但是如果我对 A 列进行排序,我不希望重新排列所有其他列。我需要所有这些列表按字母顺序单独排序。我知道可以一一进行,但我有 278 列。有没有办法让它自动化?
回答by Andy G
If there are no blank cells in any of the columns' data then the following is a slightly different approach which doesn't assume a maximum for the number of rows.
如果任何列的数据中都没有空白单元格,那么下面的方法略有不同,它不假设行数的最大值。
Sub SortIndividualJR()
Dim rngFirstRow As Range
Dim rng As Range
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = ActiveSheet
Set rngFirstRow = ws.Range("A1:JR1")
For Each rng In rngFirstRow
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=rng, Order:=xlAscending
'assuming there are no blank cells..
.SetRange ws.Range(rng, rng.End(xlDown))
.Header = xlYes
.MatchCase = False
.Apply
End With
Next rng
Application.ScreenUpdating = True
End Sub
There is nothing wrong with user1281385's code; as I say, this is just an alternative.
user1281385的代码没有问题;正如我所说,这只是一种选择。
AddedIf there are some blanks then modify the above code to use .SetRange ws.Range(rng, rng.Range("A1000").End(xlUp))
, changing 1000 to whatever you expect will be greater than the maximum number of data-rows.
添加如果有一些空白然后修改上面的代码以使用.SetRange ws.Range(rng, rng.Range("A1000").End(xlUp))
,将 1000 更改为您期望的任何值将大于最大数据行数。
回答by exussum
Select the column you want to sort then press "sort and filter" a box will come up saying
选择要排序的列,然后按“排序和过滤”,会出现一个框说
Expand selection
continue with current selection
Choose continue with current selection
选择继续当前选择
Then choose how you want it sorted. Repeat for each column you want to sort.
然后选择你想要的排序方式。对要排序的每一列重复此操作。
Expand selection is the default option
扩展选择是默认选项
Macro
宏
Dim oneRange as Range Dim aCell as Range For I = 1 to 278 Set oneRange = Range("r1c" & I & ":r1000c" & I) Set aCell = Range("r1c" & I) oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes Next I
Should work
应该管用