我正在尝试编写一些代码,该代码将删除该行中任何单元格中未找到字符“或”或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
答案 0 :(得分: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
还要注意约翰对OP的评论,您(错误地)在需要与返回字符串的"Chr(44)"
函数的求值比较时与字符串文字Chr(44)
进行比较。 1}}。