Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 根据2个条件将行复制到另一个工作表_Excel_Vba - Fatal编程技术网

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

Excel 根据2个条件将行复制到另一个工作表,excel,vba,Excel,Vba,拥有一份每天都在增长的文档。我想通过将包含发票金额的行移动到工作簿中的第二张工作表,来归档包含发票金额的行(我认为列d是数字)和余额为0的行。我的问题有两个:我需要什么代码来执行此操作?宏是否可以主动运行?这意味着当两个条件都满足时,行将自动移动到第二张工作表,还是需要添加一个按钮来每天运行宏?数据集如下所示,我希望宏将第2行和第4行取出存档: Date Client Inv# Inv Amt Deposit Payment Balance 9/2/14 A

拥有一份每天都在增长的文档。我想通过将包含发票金额的行移动到工作簿中的第二张工作表,来归档包含发票金额的行(我认为列d是数字)和余额为0的行。我的问题有两个:我需要什么代码来执行此操作?宏是否可以主动运行?这意味着当两个条件都满足时,行将自动移动到第二张工作表,还是需要添加一个按钮来每天运行宏?数据集如下所示,我希望宏将第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包含要筛选的数据。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
如果行中包含标题,或者如果行中包含和singleInventoryAmount以及零singleBalance,则复制该行

表2的输出


已经有很多这样的例子了,所以我在发布前仔细查看了它们,找不到任何关于2个标准的东西
Set wsDestination = Sheets("Sheet2")
Set objUsedRows = Sheets("Sheet1").UsedRange.Rows
Date    Client  Inv#    Inv Amt Deposit Payment Balance
9/2/2014    ABC 1003    500     500 0
9/4/2014    DEF 1005    1000    1000        0