我记录了一个宏,选出十大最大的交易,先按十个买入,再按十个卖出。根据D列对数据进行排序后,它复制交易信息并将其粘贴到另一个单元格中。
然后按列E进行排序,以获得最大的销售量,并复制相同的数据范围以粘贴到另一个单元格中。
问题是它会复制错误的信息,因为它不能同时按D列和E列对数据进行排序。如何使宏复制和粘贴正确的信息?
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发布于 2017-02-22 20:26:24
如果您使用宏记录器记录这些步骤,您会发现它通过简单地在两种类型之间包括以下行来实现它:
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear在你的情况下
Activesheet.AutoFilter.Sort.SortFields.Clear并且应该在尝试.Add新的SortField (即E列的那个)之前放置(宏记录器还在第一个Add之前插入行,只是为了安全起见)。
发布于 2017-02-22 20:16:05
有一个很大的标志,比如在世界摔跤联合会-- 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我已经添加和删除了自动过滤器,所以它应该是工作的。
https://stackoverflow.com/questions/42401022
复制相似问题