【问题标题】:Copy rows to another worksheet based on 2 conditions根据 2 个条件将行复制到另一个工作表
【发布时间】:2014-09-06 15:39:32
【问题描述】:

拥有一份每天都在增长的文档。我想通过将包含发票金额(我在想 Col D ISNUMBER)和 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

【问题讨论】:

标签: excel vba


【解决方案1】:

假设如下

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循环遍历所有使用的行

singleBalance 和 singleInventoryAmount 是该特定行的各自值。检查它们以查看它们是否都是数字而不是空白。 IsNumeric 在空白单元格上为我返回了 true。

编辑行以满足您的需要

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多