Vba 如果找到内容,则删除整行
如果单元格以文本“Joe Smith”或“Jim Bob”开头,我想删除整行。这是我从另一个问题得到的代码,但需要修改:Vba 如果找到内容,则删除整行,vba,excel,Vba,Excel,如果单元格以文本“Joe Smith”或“Jim Bob”开头,我想删除整行。这是我从另一个问题得到的代码,但需要修改: Sub SimpleDeletingMechanism() Dim c As Range ' loop through all cells in range A1:A + last userd Row in column A For Each c In Range("A1:A" & Range("A" & Rows.Count).E
Sub SimpleDeletingMechanism()
Dim c As Range
' loop through all cells in range A1:A + last userd Row in column A
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
' when Joe Smith is found in cell's content
If InStr(1, c.Text, "Joe Smith", vbTextCompare) > 0 Then
' delete that row
' when Jim Bob is found in cell's content
ElseIf InStr(1, c, "Jim Bob", vbTextCompare) > 0 Then
' delete that row
End If
Next
End Sub
如果找到其中一个名称,有人能帮我填补删除行的空白吗?无论何时从集合中删除对象,都必须向后迭代,否则,随着集合被重新索引,最终会“跳过”某些元素
Sub SimpleDeletingMechanism()
Dim rng as Range
Dim c As Range
Dim i as Long
Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
For i = rng.Rows.Count to 1 Step -1 '// Iterate BACKWARDS over the collection
Set c = rng.Cells(i,1)
' when Joe Smith is found in cell's content
If InStr(1, c.Text, "Joe Smith", vbTextCompare) > 0 Then
c.EntireRow.Delete
' when Jim Bob is found in cell's content
ElseIf InStr(1, c, "Jim Bob", vbTextCompare) > 0 Then
c.EntireRow.Delete
End If
Next
End Sub
无论何时从集合中删除对象,都必须向后迭代,否则,当集合被重新索引时,最终会“跳过”某些元素
Sub SimpleDeletingMechanism()
Dim rng as Range
Dim c As Range
Dim i as Long
Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
For i = rng.Rows.Count to 1 Step -1 '// Iterate BACKWARDS over the collection
Set c = rng.Cells(i,1)
' when Joe Smith is found in cell's content
If InStr(1, c.Text, "Joe Smith", vbTextCompare) > 0 Then
c.EntireRow.Delete
' when Jim Bob is found in cell's content
ElseIf InStr(1, c, "Jim Bob", vbTextCompare) > 0 Then
c.EntireRow.Delete
End If
Next
End Sub
你有标题行吗?如果你这样做,我会使用自动筛选而不是循环,你有标题行吗?如果你这样做,我会使用自动筛选而不是循环+1来解释反向迭代逻辑+1来指出“跳过”+1来解释反向迭代逻辑+1来指出“跳过”