如何跳过行副本中的特定列

时间:2017-12-28 17:25:51

标签: excel-vba vba excel

所以,我正在寻找一种跳过行副本中特定列的方法。我正在做一堆循环来从一堆可变大小的报告中复制/粘贴我想要一种方法来简单地跳过一行或一行中的一列或多列,因为我不能这样做一个entirecolumn.delete处理多余的事情并做一个反制度系统可能会被打破。我猜我喜欢的东西(据我所知不存在)就像行拷贝列3到5一样。

Dim LastRow As Long
Dim LastCell As Range

For Each Cell In Sheet10.Range("A:B") 
If Cell.Value Like "*Total*" Then
        Set Mastersheet = Sheet10
        Set Pastesheet = Sheet3
    Cell.EntireRow.Copy

    With Pastesheet
        Set LastCell = Pastesheet.Cells.Find(What:="*", LookAt:=xlPart, LookIn:=xlFormulas, _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)

        If Not LastCell Is Nothing Then
            LastRow = LastCell.Row
        End If

        Pastesheet.Cells(LastRow + 1, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    End With
End If

下一步

1 个答案:

答案 0 :(得分:0)

以下是我将如何完成您的任务,分解为步骤。

Dim rw As Range

'Step 1: Insert helper column
Columns(3).Insert

'Step 2: If any cell in columns A or B contain the word "Total", put "1" in the helper column
    For Each rw In Worksheets("Sheet10").UsedRange.Rows
        If rw.Cells(1, 1).Value Like "*Total*" Or rw.Cells(1, 2).Value Like "*Total*" Then
           rw.Cells(1, 3).Value = "1"
        End If
    Next

'Step 3: Filter using the helper column, hide the helper column and 3 other columns, copy(offset removes header row), paste to sheet3 lastrow +1
    With Range("A1").CurrentRegion
        .AutoFilter Field:=3, Criteria1:="1"
        .Columns("C:F").Hidden = True
        .Offset(1).SpecialCells(xlVisible).Copy Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Offset(1)
    End With

'Step 4: Clean up Sheet10, or macro will not work next time
    With Sheet10
        .AutoFilterMode = False
        .Columns.Hidden = False
        .Rows.Hidden = False
        .Columns(3).Delete
    End With