VBA Excel – 当列中的单元格值等于零时删除行

我有一些代码在我的VBA脚本,应该删除整行的行的B列的值等于零的代码。 不幸的是,我一直使用的代码行并没有完全按照我想要的来执行,因为如果相应行中的任何单元格都等于零,那么它将删除整行。 这是迄今为止的代码:

With Worksheets("Sheet2") For myloop = .Range("B10000").End(xlUp).Row To 1 Step -1 If .Cells(myloop, 4).Value = 0 Then .Rows(myloop).EntireRow.Delete Next myloop End With 

例如,我有一行,其中列D中的单元格等于零,并且即使列B中的单元格的值不为零,此代码也会删除整行。 我怎样才能更改代码,所以它实际上只扫描列B中的条目?

任何帮助表示感谢,并提前感谢。

用户gizlmo提供了答案:

更改.Cells(myloop, 4).Value to .Cells(myloop, 2).Value

一个AutoFilter()方法如下:

 With Worksheets("Sheet2") '<--| reference your shet With .Range("B1", .Cells(Rows.Count, "B").End(xlUp)) '<--| reference its column B range from row 1 (header) down to last not empty row If WorksheetFunction.CountBlank(.Cells) + WorksheetFunction.CountIf(.Cells, 0) = 0 Then Exit Sub '<--| exit of no empty or "zero" cells .AutoFilter Field:=1, Criteria1:=0, Operator:=xlOr, Criteria2:="" '<--| filter column B cells with "0" content .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete '<--| if any filtered cell other than headers then delete their entire rows End With .AutoFilterMode = False End With