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

时间:2016-12-27 20:25:18

标签: excel vba excel-vba

我正在尝试创建一个查找" Total net "列中的"程序运行网" 。一旦找到,宏将比较这两个单元格的行及其值匹配"总计Net" 被删除。

到目前为止,这是我的代码......

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

2 个答案:

答案 0 :(得分:2)

我认为当列" 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

答案 1 :(得分:0)

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

2000-XX-XX