VBA Excel – 有没有办法根据两栏中的标准删除行?

我一直在处理一个macros来清理生成并复制到Excel中的表单。 我已经find了一切,直到最后一个阶段,我需要删除包含多余公式的行。 例:

400000 | Smith, John| 2.4 | 5.66| =C1+D1 400001 | Q, Suzy | 4.6 | 5.47| =C2+D2 400002 | Schmoe, Joe| 3.8 | 0.14| =C3+D3 Blank | | | | #VALUE Blank | | | | #VALUE 

是否有一个公式比较列'A'和列'E',并删除'A'中没有值,但'E'值的所有行?

我已经尝试了一个基本的循环公式,完全由空的“A”列工作到永久删除行下面我想保留的信息,我发现了一个公式处理本网站上的filter(从brettdj),但我不知道如何编辑它来正确捕捉我所需要的东西。

这是我从brettdj:

 Sub QuickKill() Dim rng1 As Range, rng2 As Range, rng3 As Range Set rng1 = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious) Set rng2 = Cells.Find("*", , xlValues, , xlByRows, xlPrevious) Set rng3 = Range(Cells(rng2.Row, rng1.Column), Cells(1, rng1.Column)) Application.ScreenUpdating = False Rows(1).Insert With rng3.Offset(-1, 1).Resize(rng3.Rows.Count + 1, 1) .FormulaR1C1 = "=OR(RC1="",RC1<>"")" .AutoFilter Field:=2, Criteria1:="TRUE" .EntireRow.Delete On Error Resume Next 'in case all rows have been deleted .EntireColumn.Delete On Error GoTo 0 End With Application.ScreenUpdating = True 

其他一些信息:

我想要忽略的工作表的前5行,它们包含列标题和报告名称等

由40 ####的变化填充的行数,所以我不能简单地把macros删除行4。

我不了解macros观(很明显)如何详细解释某些部分是如何工作的。 在此先感谢您的帮助。

是的,有一种方法不需要循环

你的公式(在Excel电子表格)将是这样的:

 =IF(AND(A:A="",E:E<>""),"DELETE THIS ROW","LEAVE THIS ROW") 

现在,有可能得到这个公式在我testing的行中返回一个错误返回True。 我们这样做的原因是一个名为SpecialCells的Excel的function。

在Excel中select任何空单元格,并在公式栏types中

 =NA() 

接下来,按下F5或CTRL + G(在编辑菜单上转到… ),然后单击特殊button以显示SpecialCells对话框。

在该对话框中,单击“公式”旁边的电台,然后清除checkbox,以便只select错误 。 现在点击OK

Excel应该已经select了工作表中的所有单元格,其中有一个错误( #N / A )。

下面的代码利用这个技巧,在列H中创build一个公式,将所有要删除的行中的#N / A ,然后调用SpecialCells来查找行,并清除(删除)它们…

  Sub clearCells() ' Dim sFormula As String ' ' this formula put's an error if the test returns true, ' so we can use SpecialCells function to highlight the ' rows to be deleted! sFormula = "=IF(AND(A:A="""",E:E<>""""),NA(),"""")" ' ' put a formula into column H, to find the rows that ned to be deleted... Range("H5:H" & Range("E65536").End(xlUp).Row).Formula = sFormula ' Stop ' next line clears multiple rows, could also delete the entire row! ' now clear the cells returned by SpecialCells ... Range("H5:H" & Range("E65536").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).Offset(0, -7).Clear ' ' EntireRow .Delete shift:=xlup ' ' clean up the formula Range("H5:H" & Range("E65536").End(xlUp).Row).Clear ' End Sub 

复制粘贴,并通过使用F8步骤…

那么我们可以帮助你更多…

希望能让你开始

顺便说一句,如果你真的想要一个,也可以用一个循环 🙂

菲利普

我不知道为什么使用循环被Philip认为是一件坏事。 这是一个简单的macros,遍历每一行,并删除Column A为空, Column E有一些价值的行。

它假定您的代码在Sheet 1 。 我已经评论了代码,以便您更轻松地遵循。 如果您不想考虑删除1-5行,那么将For loop值设置为6如代码中的注释所示。

请注意,删除是从下往上进行的,因为当您删除一行时,循环中的totalRows计数发生变化。 为了避免循环丢失行,我只是按照相反的顺序进行操作。

 Public Sub DeleteWhereEmpty() Dim colARowCount As Integer Dim colERowCount As Integer Dim totalRows As Integer Dim currentRow As Integer Dim colA As Integer Dim colE As Integer Dim colACurrentValue As String Dim colECurrentValue As String colA = 1 colE = 5 colARowCount = Sheet1.Cells(Rows.Count, colA).End(xlUp).Row colERowCount = Sheet1.Cells(Rows.Count, colE).End(xlUp).Row 'set total number of rows to look at totalRows = colARowCount If colARowCount < colERowCount Then totalRows = colERowCount 'E has more rows, so use it End If 'to stop deleting at row 5, replace 'To 1' with 'To 6' For currentRow = totalRows To 1 Step -1 colACurrentValue = Cells(currentRow, colA).Value colECurrentValue = Cells(currentRow, colE).Text 'if A is empty and E has data in it, delete the row If colACurrentValue = "" Or IsEmpty(colACurrentValue) _ And Len(colECurrentValue) > 0 Then Cells(currentRow, colA).EntireRow.Delete End If Next End Sub 

之前

在这里输入图像说明

在这里输入图像说明