简体   繁体   English

excel vba 自动过滤范围并删除列 D 中所有带有 0 的行

[英]excel vba autofilter range and delete all rows with 0 in col D

I have a long list of data (product labels), another sheet has the raw data and calculations.我有一长串数据(产品标签),另一张表有原始数据和计算。 I have a macro that imports the data in A2 - AP2.我有一个宏可以导入 A2 - AP2 中的数据。 It scans Col D and deletes any row that has a zero (0) value in col D. I have it working but, is there some way to change the code to only delete the row from A to AP?它扫描列 D 并删除列 D 中具有零 (0) 值的任何行。我可以正常工作,但是,是否有某种方法可以更改代码以仅删除从 A 到 AP 的行? I have 3 other codes that need this but I cant just delete an entire row due to other data / helper columns?我还有 3 个其他代码需要这个,但由于其他数据/辅助列,我不能删除整行?

Worksheets("Labels").Activate
 ActiveSheet.AutoFilterMode = False

 ActiveSheet.Range(drop2).AutoFilter Field:=4, Criteria1:="=-", _
        Operator:=xlOr, Criteria2:="=+"

    Dim oRow As Range, rng As Range
    Dim myRows As Range
    With Sheets("Labels")
        Set myRows = Intersect(.Range("D:D").EntireRow, .UsedRange)
        If myRows Is Nothing Then Exit Sub
    End With

    For Each oRow In myRows.Columns(1).Cells
        If oRow.EntireRow.Hidden Then
            If rng Is Nothing Then
                Set rng = oRow
            Else
                Set rng = Union(rng, oRow)
            End If
        End If
    Next

    If Not rng Is Nothing Then rng.EntireRow.Delete

 ActiveSheet.AutoFilterMode = False
 Application.ScreenUpdating = True

I can't guess what your doing in the code without seeing your excel data.如果没有看到您的 excel 数据,我无法猜测您在代码中做了什么。 But, based on what you explained, you will look through column D and if you find 0 then you will delete the entire row.但是,根据您的解释,您将查看 D 列,如果发现 0,则将删除整行。 But, you don't want to delete the entire row as that will delete the helper column.但是,您不想删除整行,因为这会删除辅助列。 I am just trying to repeat what you said.我只是想重复你说的话。 If this is what you want, use range Delete function call with Shift:=xlUp如果这是您想要的,请使用带有 Shift:=xlUp 的范围删除函数调用

Range("A2:AP2").Delete Shift:=xlUp

This will not delete the entire row, but will only delete data in A2:AP2 and shift all the below cells up.这不会删除整行,而只会删除 A2:AP2 中的数据并将下面的所有单元格向上移动。 Kind of deleting your entire row and leaving your helper columns untouched.有点删除你的整行并保持你的辅助列不变。

Update 1:更新 1:

I will try to explain it with a small example.我将尝试用一个小例子来解释它。 Below screenshot shows the sample data I am using,下面的屏幕截图显示了我正在使用的示例数据,

在此处输入图片说明

The yellow marked cells have zero values and the entire rows need to be cleared out except the helper column data.黄色标记的单元格的值为零,除辅助列数据外,整行都需要清除。 Below is the sample code for this excel data,下面是这个excel数据的示例代码,

Dim i As Integer
Dim myRows As Range

Set myRows = Intersect(Sheet1.Range("A:E").EntireRow, Sheet1.UsedRange)
If myRows Is Nothing Then Exit Sub

For i = 1 To myRows.Rows.Count
    If Sheet1.Cells(i, 4) = 0 Then
        Sheet1.Range("A" & CStr(i) & ":E" & CStr(i)).Delete (xlUp)
    End If
Next

If the above code gets executed, below is the screenshot of the result.如果上面的代码被执行,下面是结果的屏幕截图。 Note that the entire row of column D with 0 value got deleted leaving Helper columns unaffected.请注意,具有 0 值的 D 列的整行都被删除了,而 Helper 列不受影响。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM