2017-02-22 31 views
0

我录一个宏挑的前十名最大的行业排序第一十个购买然后十销售。根据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 

回答

1

如果录制使用宏录制这些步骤,你会发现,它简单的包含两类之间以下行实现它:

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

在你的情况,这将是

Activesheet.AutoFilter.Sort.SortFields.Clear 

,并且应该在尝试.Add之前放置新的SortField,即用于列E的那个。(宏记录器还在第一个Add之前插入该行,以确保安全。)

+0

此使用“清除”命令工作。谢谢你的帮助 –

2

有了一个大牌子,就像在世界摔跤联合会 - 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 

我已经添加和删除自动筛选,所以它应该是工作。