Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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 Excel将删除该行_Vba_Excel - Fatal编程技术网

如果行中的一个单元格以某个字符串开头,则VBA Excel将删除该行

如果行中的一个单元格以某个字符串开头,则VBA Excel将删除该行,vba,excel,Vba,Excel,我有两列,其中包含一些组件的规范。 基本上,如果两个指定列的首字母与S不同,我希望删除整行 我的桌子看起来像这样 如果列“from device”和“to device”以G或C开头(或者,更具体地说,如果列“from”和“to”以S开头,则保留整行),我想删除每一行,我的代码如下: Sub FilterData2() Dim rcount As Long With Sheet3 rcount = Range("B" & Rows.Count).End(xlUp).Row

我有两列,其中包含一些组件的规范。 基本上,如果两个指定列的首字母与S不同,我希望删除整行

我的桌子看起来像这样

如果列“from device”和“to device”以G或C开头(或者,更具体地说,如果列“from”和“to”以S开头,则保留整行),我想删除每一行,我的代码如下:

Sub FilterData2()

Dim rcount As Long

With Sheet3
    rcount = Range("B" & Rows.Count).End(xlUp).Row
    With Range("E1:E" & rcount)
        If Application.WorksheetFunction.CountIf(Columns(5), "C*") > 0 Then
            .AutoFilter field:=1, Criteria1:="C*"
            .Offset(1).Resize(.Rows.Count - 1, 1).EntireRow.Delete
            .AutoFilter
        End If
        If Application.WorksheetFunction.CountIf(Columns(5), "G*") > 0 Then
             .AutoFilter field:=1, Criteria1:="G*"
             .Offset(1).Resize(.Rows.Count - 1, 1).EntireRow.Delete
             .AutoFilter
        End If
       End With
   End With

End Sub
但是,这只适用于列E,这意味着如果列G包含以S开头的单元格,而列E不包含,则该行仍将被删除,我希望保留该行


有什么建议吗?谢谢

您可以在VBA中组合if语句

使用和修改器:

If Application.WorksheetFunction.CountIf(Columns(5), "C*") > 0 AND Application.WorksheetFunction.CountIf(Columns(5), "G*") > 0 Then
If Application.WorksheetFunction.CountIf(Columns(5), "C*") > 0 OR Application.WorksheetFunction.CountIf(Columns(5), "G*") > 0 Then
类似地,可以使用或修改器:

If Application.WorksheetFunction.CountIf(Columns(5), "C*") > 0 AND Application.WorksheetFunction.CountIf(Columns(5), "G*") > 0 Then
If Application.WorksheetFunction.CountIf(Columns(5), "C*") > 0 OR Application.WorksheetFunction.CountIf(Columns(5), "G*") > 0 Then
要将此应用于代码,您只想查看单元格内容是否以“从”或“到”中的s开头:

Dim rcount As Long
Dim i As Integer
Dim strE, strG As String

With Sheets("Sheet3")
    rcount = .Range("B" & Rows.Count).End(xlUp).Row

    For i = rcount to 2 Step -1
        strE = Left(.Cells(i, "E").Value, 1)
        strG = Left(.Cells(i, "G").Value, 1)
        If strE = "S" Or strG = "S" Then
        Else
            .Rows(i).EntireRow.Delete
        End If
    Next i
End With

这将大大简化过程。

您使用的If语句仅在E列
中包含范围(“E1:E”&rcount)
我尝试使用instr,但它几乎删除了所有内容,因为几乎每个单元格都包含一个“C”或“G”,我不知道如何指定我希望它仅用于第一个字符这很好!我试着按照一位朋友的建议使用Left,但我仍然在弄清楚它的语法。谢谢大家!@杰安卡洛不用担心。我可能会建议做一个更改,因为即使在处理代码时我没有注意到这一点:rcount=.cells(.rows.count,“B”).End(xlUp)。我没有注意到.rows.count也没有指定工作表(“Sheet3”)。