将多个列复制到另一个工作表

时间:2014-08-16 10:27:15

标签: xml excel excel-vba vbc vba

我的XML feed有很多不必要的数据,所以我找到了以下宏来复制行,其中包括" Soccer"在BA栏中,到新工作表 我想要做的只是将特定列E,I,P,T,W,Y,Z复制到工作表而不是整行,因此我不必隐藏那些不必要的列。

Sub SearchForString()

Dim LSearchRow As Integer
Dim LCopyToRow As Integer

On Error GoTo Err_Execute

'Start search in row 3
LSearchRow = 3

'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 2

While Len(Range("A" & CStr(LSearchRow)).Value) > 0

    'If value in column BA = "Soccer", copy entire row to Sheet2
    If Range("BA" & CStr(LSearchRow)).Value = "Soccer" Then

        'Select row in Sheet1 to copy
        Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
        Selection.Copy

        'Paste row into Sheet2 in next row
        Sheets("Sheet2").Select
        Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
        ActiveSheet.Paste

        'Move counter to next row
        LCopyToRow = LCopyToRow + 1

        'Go back to Sheet1 to continue searching
        Sheets("Sheet1").Select

    End If

    LSearchRow = LSearchRow + 1

Wend

'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select

MsgBox "All matching data has been copied."

Exit Sub

Err_Execute:
    MsgBox "An error occurred."

End Sub

1 个答案:

答案 0 :(得分:0)

请改用数据透视表。将E,I,P,T,W,Y,Z列添加为数据透视表中的列,并通过“Soccer”添加数据透视表过滤器。