如果在该行中找不到字符","或Chr(44),则删除整行.重复所有行

Val*_*org 1 excel vba excel-vba excel-formula

我尝试写一些代码,将删除其中的人物""或CHR(44)不以任何该行中的细胞上发现的任何行.

我已经做到这一点,但我正在努力,因为代码只搜索C列","但我需要它来搜索整个当前行.

我该如何更新?

Sub DeleteRows()
' Defines variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long


' Defines LastRow as the last row of data based on column C
LastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).row


' Sets check range as C1 to the last row of C
Set cRange = Range("C1:C" & LastRow)


' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
    With cRange.Cells(x)
        ' If the cell does not contain one of the listed values then...
        If .value <> "Chr(44)" Then
            ' Delete that row
            .EntireRow.Delete
        End If
    End With
' Check next cell, working upwards
Next x


End Sub
Run Code Online (Sandbox Code Playgroud)

Dav*_*ens 6

可能更容易使用对象的Find方法Range,而不是逐个细胞迭代.并且您可以使用EntireRow每个单元格范围的属性来执行此操作:

For x = cRange.Cells.Count To 1 Step -1
    With cRange.Cells(x).EntireRow
        If .Find(Chr(44)) Is Nothing Then
            .Delete
        End If
    End With
Next
Run Code Online (Sandbox Code Playgroud)

还要注意每个John对OP的评论,"Chr(44)"当你需要与Chr(44)函数的评估进行比较时,你(错误地)与字符串文字进行比较,函数评估返回一个字符串:",".