macros改变数据覆盖以前的数据

我logging了一个macros观的十大买卖排名前十的交易,然后十个卖出。 根据D列sorting数据后,它复制交易信息并将其粘贴到另一个单元格中。
然后按列Esorting以获得最大的销售额,并复制相同的数据范围以粘贴到另一个单元格中。
问题是它会复制错误的信息,因为它不能同时按列D和E对数据进行sorting。 我如何让macros复制并粘贴正确的信息?

Sub ttt() ' ' ttt Macro ' top ten trades output ' ' Keyboard Shortcut: Ctrl+Shift+T ' buys Rows("3:3").Select Selection.AutoFilter ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("D3" _ ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _ xlSortTextAsNumbers With ActiveSheet.AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A5:I14").Select Selection.Copy Range("K3").Select ActiveSheet.paste Application.CutCopyMode = False ' sells ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("E3" _ ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _ xlSortTextAsNumbers With ActiveSheet.AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A5:I14").Select Selection.Copy Range("u3").Select ActiveSheet.paste Application.CutCopyMode = False End Sub 

如果使用macroslogging器logging这些步骤,您会发现只需在两种types之间包括以下行即可实现:

 ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear 

在你的情况下,将是

 Activesheet.AutoFilter.Sort.SortFields.Clear 

并且应该在尝试添加之前放置。添加新的SortField ,即列E的macros。(macros录制器也在第一次Add之前插入行,以保证安全)。

像世界摔跤联合会那样有一个很大的标志 – do not do this at home – 你可以这样做:

 Sub ttt() ' ' ttt Macro ' top ten trades output ' ' Keyboard Shortcut: Ctrl+Shift+T ' buys Rows("3:3").Select Selection.AutoFilter ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("D3" _ ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _ xlSortTextAsNumbers With ActiveSheet.AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A5:I14").Select Selection.Copy Range("K3").Select ActiveSheet.paste Application.CutCopyMode = False ' sells Rows("3:3").AutoFilter Rows("3:3").AutoFilter ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("E3" _ ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _ xlSortTextAsNumbers With ActiveSheet.AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("A5:I14").Select Selection.Copy Range("u3").Select ActiveSheet.paste Application.CutCopyMode = False End Sub 

我已经添加并删除了自动filter,所以它应该工作。