假设如下
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