VBA:如果值匹配,则删除一行

我试图创build一个macros在C列寻找“总净额 ”和“程序运行networking” 。一旦定位,macros比较这两个单元格的行和他们的价值观匹配行然后“总净”得到删除。

这是我的代码到目前为止…

Sub DeletingEmptyPages() Dim WS As Worksheet For Each WS In Sheets Dim Mystring As String Dim MystringII As String MystringII = "Total Net" Mystring = "Program Operating Net" Dim n As Long Dim nlast As Long Dim rw As Range Set rw = ActiveWorkbook.ActiveSheet.UsedRange.Rows nlast = rw.count For n = nlast To 9 Step -1 If (Column(c).Value = MystringII And Column(c).Value = Mystring) Then rw.Rows(n).Delete End If Next n Next WS End Sub 

我想,当两个string存在列“C”,你想比较列“A”和“B”在两个行是相等的。 如果需要在匹配的行上比较更多列,可以使用以下代码并轻松调整它:

 Sub Delete_DuplicateTotalNet() Dim WS As Worksheet, row1 As Long, row2 As Long For Each WS In Sheets With WS On Error Resume Next row1 = WorksheetFunction.Match("Total Net", .Columns("C"), 0) row2 = WorksheetFunction.Match("Program Operating Net", .Columns("C"), 0) If Err.Number <> 0 Then GoTo NextWS If .Range("A" & row1).Value = .Range("A" & row2).Value And _ .Range("B" & row1).Value = .Range("B" & row2).Value Then .Rows(row1).Delete End If End With NextWS: Err.Clear Next WS End Sub 

这是它的样子,但会导致运行时错误

  Sub Delete_DuplicateTotalNet() Dim WS As Worksheet, row1 As Long, row2 As Long, rng As Long For Each WS In Sheets On Error GoTo NextWS With WS If WS.Visible = xlSheetVisible Then row1 = WorksheetFunction.Match("Total Net", .Columns(3), 0) row2 = WorksheetFunction.Match("Program Operating Net", .Columns(3), 0) If .Range("D" & row1).Value = .Range("D" & row2).Value And _ .Range("E" & row1).Value = .Range("E" & row2).Value And _ .Range("F" & row1).Value = .Range("F" & row2).Value And _ .Range("G" & row1).Value = .Range("G" & row2).Value And _ .Range("H" & row1).Value = .Range("H" & row2).Value And _ .Range("I" & row1).Value = .Range("I" & row2).Value And _ .Range("J" & row1).Value = .Range("J" & row2).Value And _ .Range("K" & row1).Value = .Range("K" & row2).Value Then .Rows(row1).Delete End If End If End With NextWS: Err.Clear Next WS End Sub