复制粘贴Excel数据

时间:2014-11-27 14:18:01

标签: excel vba

我有2张,sheet1和sheet2。从sheet1数据我必须复制数据并粘贴在sheet2中,然后再次从sheet1我必须复制另一组不同的数据并粘贴在sheet2的最后一行,其中我第一次粘贴数据

Sub Copy_chains_to_other_sheet()
ActiveSheet.Range("$A$1").AutoFilter Field:=8, Criteria1:="<>1", _
Operator:=xlAnd
ActiveSheet.Range("$A$1:$I$681").AutoFilter Field:=1, Criteria1:="=*antaris*" _
    , Operator:=xlAnd
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
ActiveSheet.Next.Select
ActiveSheet.Paste
ActiveSheet.Range("$A$1").AutoFilter Field:=1

End Sub

这是我写的宏,但我不知道如何继续。因为有一次我有5行数据,那时我需要从sheet1复制数据并粘贴在sheet2中并使用下一组数据我需要将其粘贴到第6行,但另一次我有8行数据我需要的时间从第9行开始粘贴下一组数据,以便如何处理。

提前感谢您的回答。

此致 Vignesh

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望将连续自动过滤数据的结果从Sheet1复制到连续的&#34;列表&#34;在Sheet2中。如果是这样,那么也许尝试以下方法让你前进。你需要改变变量/名称以满足你的要求,我做了一些假设。

Option Explicit

Sub copyAFs()
Dim wsONE As Worksheet, wsTWO As Worksheet
Dim ONEstrow As Long, ONEendrow As Long, ONEstcol As Long, ONEendcol As Long
Dim TWOstrow As Long, TWOnextrow As Long, TWOstcol As Long
Dim crit1col As Long, crit2col As Long
Dim crit1 As String, crit2 As String

Set wsONE = Sheets("Sheet1")
Set wsTWO = Sheets("Sheet2")
ONEstrow = 1
ONEstcol = 1
ONEendcol = 10
TWOstrow = 1
TWOstcol = 1
crit1 = "antaris"
crit2 = "1"
crit1col = 1
crit2col = 8

    With wsTWO
        TWOnextrow = .Cells(.Rows.Count, TWOstcol).End(xlUp).Row + 1
    End With

'clear autofilter
wsONE.AutoFilterMode = False

    'apply autofilter
    With wsONE
        ONEendrow = Cells(Rows.Count, ONEstcol).End(xlUp).Row + 1
            With .Range(.Cells(ONEstrow, ONEstcol), .Cells(ONEendrow, ONEendcol))
                'set autofilter
                .AutoFilter Field:=crit1col, Criteria1:=crit1
                .AutoFilter Field:=crit2col, Criteria1:=crit2
            End With
    End With

    'copy filtered range without header
    With wsTWO
        wsONE.AutoFilter.Range.Offset(1, 0).Copy Destination:=.Range(.Cells(TWOnextrow, TWOstcol), .Cells(TWOnextrow, TWOstcol))
    End With

    'clear autofilter
    wsONE.AutoFilterMode = False

End Sub