根据2个条件将行复制到另一个工作表

时间:2014-09-06 15:39:33

标签: excel vba

每天都有一份长度增长的文件。我想将包含发票金额(我认为是D DN)的行和0余额存档,方法是将它们移动到工作簿中的第二个工作表。我的问题是双重的:我需要做什么代码,并且宏可以主动运行(意味着当两个条件都满足时行自动移动到第二张表),或者我是否需要添加一个按钮来运行宏观每日?数据集如下,我喜欢宏取第2行和第2行。 4出来存档:

Date    Client    Inv#    Inv Amt    Deposit    Payment    Balance
9/2/14  ABC       1003    $500                  $500       $0
9/4/14  ABC       1004    $400                             $400
9/4/14  DEF       1005    $1000      $1000                 $0
9/5/14  DEF       1006    $4500      $2000                 $2500
9/5/14  ABC       1007    $650                             $650
9/6/14  GHI       1008    $2500      $1500                 $1000
9/6/14  ABC       1009    $800

1 个答案:

答案 0 :(得分:0)

假设以下

Sheet1包含要过滤的数据。 Sheet2是一个表,您将把数据粘贴到其中。我们将对标题的存在进行纠错

Public Sub MoveSomeStuff()
    Const intDateOffset As Integer = 1
    Const intBalanceOffset As Integer = 7
    Const intInventoryAmountOffset As Integer = 4

    Dim objSingleRow As Range
    Dim objUsedRows As Range
    Dim objEmptyRow As Range
    Dim wsDestination As Worksheet

    Dim singleBalance As Long
    Dim singleInventoryAmount As Long

    Set wsDestination = Sheets("Sheet2")
    Set objUsedRows = Sheets("Sheet1").UsedRange.Rows

    ' Stop screen updating while the code is processing. Will speed it up.
    Application.ScreenUpdating = False

    ' Cycle through every row that is used.
    For Each objSingleRow In objUsedRows

        ' Gather Inventory Amount and Balance
        singleBalance = IIf((IsNumeric(objSingleRow.Cells(, intBalanceOffset).Value) And (objSingleRow.Cells(, intBalanceOffset).Value <> "")), objSingleRow.Cells(, intBalanceOffset).Value, -1)
        singleInventoryAmount = IIf((IsNumeric(objSingleRow.Cells(, intInventoryAmountOffset).Value) And (objSingleRow.Cells(, intInventoryAmountOffset).Value <> "")), objSingleRow.Cells(, intInventoryAmountOffset).Value, -1)


        ' Determine if this row should be copied
        If (objSingleRow.Cells(, intDateOffset).Value = "Date") Or _
                ((singleInventoryAmount > 0) And (singleBalance = 0)) Then
            ' Copy the row to the clipboard
            objSingleRow.Copy

            With wsDestination
                ' If the sheet has not data just paste in the first row.
                If .Range("A1") = "" Then
                    .Range("A1").PasteSpecial (xlPasteAll)
                Else
                    ' Locate the next empty row that we can paste into.
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteAll)
                End If
            End With
        End If
    Next

    ' Resume screen updating.
    Application.ScreenUpdating = True
End Sub

循环显示Sheet1

中所有已使用的行

singleBalancesingleInventoryAmount是该特定行的相应值。检查它们是否都是数字而不是空白。 IsNumeric在空白单元格上为我返回了真实。

根据您的需要编辑线条

Set wsDestination = Sheets("Sheet2")
Set objUsedRows = Sheets("Sheet1").UsedRange.Rows

如果一行包含标题,或者如果包含singleInventoryAmount和零singleBalance

,则复制一行

Sheet2上的输出

Date    Client  Inv#    Inv Amt Deposit Payment Balance
9/2/2014    ABC 1003    500     500 0
9/4/2014    DEF 1005    1000    1000        0