Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA:搜索子字符串并删除整行_Vba_Excel - Fatal编程技术网

VBA:搜索子字符串并删除整行

VBA:搜索子字符串并删除整行,vba,excel,Vba,Excel,我试图删除p列中包含“H”字符串的所有行。宏可以工作,但是每次只删除一半必要的行。这是因为代码中的For循环——删除一行时,下一行的i值将与删除的行相同,并被next i跳过 Dim LastRow As Long 'Finds last row With ActiveSheet LastRow = .Cells(.Rows.count, "P").End(xlUp).Row End With 'Iterates through rows in column B, and delet

我试图删除
p
列中包含“H”字符串的所有行。宏可以工作,但是每次只删除一半必要的行。这是因为代码中的
For
循环——删除一行时,下一行的
i
值将与删除的行相同,并被
next i
跳过

Dim LastRow As Long

'Finds last row
With ActiveSheet
    LastRow = .Cells(.Rows.count, "P").End(xlUp).Row
End With

'Iterates through rows in column B, and deletes the row if string contains "H"
For i = 4 To LastRow
    If InStr(1, Range("P" & i), "H") <> 0 Then Rows(i).EntireRow.Delete
Next i

'Message Box when tasks are completed
  MsgBox "Complete"
Dim LastRow尽可能长
'查找最后一行
使用ActiveSheet
LastRow=.Cells(.Rows.count,“P”).End(xlUp).Row
以
'遍历列B中的行,如果字符串包含“H”,则删除该行
对于i=4到最后一行
如果InStr(1,范围(“P”和“i”),“H”)为0,则行(i).EntireRow.Delete
接下来我
'任务完成时的消息框
MsgBox“完成”

如果删除一行以获得所有行,是否有方法让
For
循环重复相同的
i
值?

标准方法是按相反顺序迭代

Dim LastRow As Long

'Finds last row
With ActiveSheet
    LastRow = .Cells(.Rows.count, "P").End(xlUp).Row
End With

'Iterates in reverse through rows in column B, and deletes the row if string contains "H"
For i = LastRow To 4 Step -1 
    If InStr(1, Range("P" & i), "H") <> 0 Then Rows(i).EntireRow.Delete
Next i

'Message Box when tasks are completed
  MsgBox "Complete"
Dim LastRow尽可能长
'查找最后一行
使用ActiveSheet
LastRow=.Cells(.Rows.count,“P”).End(xlUp).Row
以
'反向遍历列B中的行,如果字符串包含“H”,则删除该行
对于i=最后一行到4步骤-1
如果InStr(1,范围(“P”和“i”),“H”)为0,则行(i).EntireRow.Delete
接下来我
'任务完成时的消息框
MsgBox“完成”

尝试筛选通配符
*H*
并删除可见行

Option Explicit

    Sub qweqrwtqrweq()
        if autofiltermode then .autofiltermode = false
        With ActiveSheet  '<~~much better to use thge ACTUAL WORKSHEET NAME!! e.g. with worksheets("sheet1")
            With .Range(.Cells(4, "P"), .Cells(.Rows, Count, "P").End(xlUp))
                .AutoFilter fiedl:=1, Criteria1:="*h*"
                If CBool(Application.subtotla(103, .Cells)) Then
                    .Cells.etirerow.Delete
                End If
            End With
        End With
        if autofiltermode then .autofiltermode = false
        MsgBox "Complete"
    End Sub
选项显式
子qweqrwtqrweq()
如果为autofiltermode,则.autofiltermode=false
使用ActiveSheet'