简体   繁体   English

删除不包含字符串的行

[英]Delete rows that do not contain string

I am trying to search a table generated by another module and remove rows that do not contain the string "REPAIR_RTS" . 我正在尝试搜索另一个模块生成的表,并删除不包含字符串"REPAIR_RTS" I have tried several methods from this site but I get type mismatches every time I change the search value to "REPAIR_RTS" (the line that says CASE...). 我已经从该站点尝试了几种方法,但是每次将搜索值更改为"REPAIR_RTS" (显示为CASE的行)时,都会出现类型不匹配的情况。

Below is my latest attempt. 以下是我的最新尝试。

Public Sub Test()
lastRow = Range("D65000").End(xlUp).row
For i = lastRow To 2 Step -1
Select Case Cells(i, 4).Value
    Case "REPAIR_RTS"
        'Do nothing
    Case Else
        Cells(i, 8).EntireRow.Delete
End Select
Next i
End Sub

I am still new to VBA so I am having a very difficult time troubleshooting this. 我还是VBA的新手,所以我很难解决此问题。 I am open to other methods as well, I am trying to learn new methods on the fly as I continue in my first analyst job. 我也对其他方法持开放态度,在我继续执行第一份分析师工作时,我正在尝试快速学习新方法。

The simplest way around this is to test for errors first... 解决此问题的最简单方法是先测试错误...

err... no. 嗯...不

Simply change your Cells(i, 4).Value to Cells(i, 4).Text . 只需将您的Cells(i, 4).Value更改为Cells(i, 4).Text

Still, .Text can be a pain as it returns exactly what is shown. 不过, .Text可能会很痛苦,因为它会准确返回所显示的内容。

A bit less easy is the change from Cells(i, 4).Value to CStr(Cells(i, 4).Value) . Cells(i, 4).Value更改为CStr(Cells(i, 4).Value)不太容易。 However, it should do exactly what you want. 但是,它应该完全满足您的要求。

If you still have any Questions, just ask :D 如果您还有任何问题,请问:D

You can't compare an error value (such as #N/A ) with a string - that is what is generating your "type mismatch" error. 您无法将错误值(例如#N/A )与字符串进行比较-这就是生成“类型不匹配”错误的原因。

The simplest way around this is to test for errors first, then continue testing for the specific string if it isn't an error: 解决此问题的最简单方法是先测试错误,然后在没有错误的情况下继续测试特定的字符串:

Public Sub Test()
    Dim lastRow As Long
    Dim i As Long
    With Worksheets("Chargable Vendors")
        lastRow = .Cells(.Rows.Count, "D").End(xlUp).row
        For i = lastRow To 2 Step -1
            If IsError(.Cells(i, 4).Value) Then
                .Rows(i).Delete
            Else
                Select Case .Cells(i, 4).Value
                    Case "REPAIR_RTS"
                        'Do nothing
                    Case Else
                        .Rows(i).Delete
                End Select
            End If
        Next i
    End With
End Sub

An alternative would be to include, within the Select Case statement, the specific tests for each of the error values prior to testing for the string, eg 一种替代方法是在Select Case语句内包括在测试字符串之前针对每个错误值的特定测试,例如

Select Case .Cells(i, 4).Value
    Case CVErr(xlErrNA), CVErr(xlErrDiv0), CVErr(xlErrName), CVErr(xlErrValue) 'etc
        .Rows(i).Delete
    Case "REPAIR_RTS"
        'Do nothing
    Case Else
        .Rows(i).Delete
End Select

but I find it easier to do the single IsError to test for all of them. 但是我发现执行单个IsError来测试所有这些对象更容易。


Note: 注意:

This could also be done with an If statement rather than a Select Case statement: 也可以使用If语句而不是Select Case语句来完成:

Public Sub Test()
    Dim lastRow As Long
    Dim i As Long
    With Worksheets("Chargable Vendors")
        lastRow = .Cells(.Rows.Count, "D").End(xlUp).row
        For i = lastRow To 2 Step -1
            If IsError(.Cells(i, 4).Value) Then
                .Rows(i).Delete
            ElseIf .Cells(i, 4).Value <> "REPAIR_RTS" Then
                .Rows(i).Delete
            End If
        Next i
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM