循环macros,单独在所有列上工作,而不用编写长长的重复过渡列表

我试图循环这个macros(下)经过我所有的数据列,但需要一些帮助创build一个循环或调整当前的代码工作的所有列。

Sub Trial_5() ' ActiveCell.Offset(0, -7).Columns("A:A").EntireColumn.Select ActiveWorkbook.Worksheets("Sheet6").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet6").Sort.SortFields.Add Key:=ActiveCell, _ SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet6").Sort .SetRange ActiveCell.Range("A1:A16395") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom `enter code here`.SortMethod = xlPinYin .Apply End With End Sub 

我是否将.columns或activecell.offset调整到总范围?

调整脚本

 Sub eachcolumndesending() ' ' eachcolumndesending Macro ' descending ' ' Columns("A:A").Select ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add Key:=Range("A1"), _ SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet5").Sort .SetRange Range("A2:A32") .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Columns("B:B").Select ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add Key:=Range("B1"), _ SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet5").Sort .SetRange Range("B1:B33") .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

我build议你从logging的代码移到VBA实际需要的单列sorting。

 Sub sortAllColumns() Dim c As Long On Error Resume Next '<~~ may be necessary if a column breaks the sort With Worksheets("Sheet5") For c = .UsedRange.Columns.Count To 1 Step -1 With .Columns(c) .Cells.Sort Key1:=.Columns(1), Order1:=xlDescending, _ Orientation:=xlTopToBottom, Header:=xlGuess End With Next c End With End Sub 

顺便说一句,你可能不应该有一个标题的存在xlGuess。 无论是(xlYes)还是不是(xlNo),但你知道你的数据比我好。