2015-04-02 89 views
0

下面的代码使用一个范围从工作表中删除行,我的worbook没有标准化,所以我的问题是如何在这里使用InStr函数也删除类似的值,即如果“苹果有限公司”在范围内,那么它会删除包含“苹果”的行或反之亦然......是否有可能?使用InStr函数来删除范围内的相似值

更新:第28行:需要object @freeman我无法获得“Dim Cel as Range”的工作,我已经插入到这里并在工作表的顶部插入了相同的结果。你写的'Cel'的语法是?下面

Sub Loop_Example() 
    Dim ChkRange As Range 
     Dim Firstrow As Long 
     Dim Lastrow As Long 
     Dim Lrow As Long 
     Dim CalcMode As Long 
     Dim ViewMode As Long 
    Set ChkRange = Sheets("Sheet2").Range("A1:A13") 
     With Application 
      CalcMode = .Calculation 
      .Calculation = xlCalculationManual 
      .ScreenUpdating = False 
     End With 
     With Sheet1 
      .Select 
      ViewMode = ActiveWindow.View 
      ActiveWindow.View = xlNormalView 
      .DisplayPageBreaks = False 
      Firstrow = 2 
      Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 
      For Lrow = Lastrow To Firstrow Step -1 
       With .Cells(Lrow, "A") 
        If Not IsError(.Value) Then 
    If Not IsError(Application.Match(.Value, ChkRange, 0)) Then 
    .EntireRow.Delete 
    End If 
    For Each Cell In ChkRange 
     If InStr(1, Cell.Value, .Value) > 0 And Cell.Value <> .Value Then 
     'include the 2nd part of the "AND" to ensure you don't delete based 
     'on a COMPLETE match, only on a partial match as noted in the comments 
     Cell.EntireRow.Delete 
    End If 
    Next 
End If 

End With 
    Next Lrow 
     End With 
ActiveWindow.View = ViewMode 
With Application 
      .ScreenUpdating = True 
      .Calculation = CalcMode 
     End With 
    End Sub 

Sub Loop_Example() 
     Dim ChkRange As Range 
      Dim Firstrow As Long 
      Dim Lastrow As Long 
      Dim Lrow As Long 
      Dim CalcMode As Long 
      Dim ViewMode As Long 
     Set ChkRange = Sheets("Sheet2").Range("A1:A13") 
      With Application 
       CalcMode = .Calculation 
       .Calculation = xlCalculationManual 
       .ScreenUpdating = False 
      End With 
      With Sheet1 
       .Select 
       ViewMode = ActiveWindow.View 
       ActiveWindow.View = xlNormalView 
       .DisplayPageBreaks = False 
       Firstrow = 2 
       Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 
       For Lrow = Lastrow To Firstrow Step -1 
        With .Cells(Lrow, "A") 
         If Not IsError(.Value) Then 
     If Not IsError(Application.Match(.Value, ChkRange, 0)) Then 
     .EntireRow.Delete 
     End If 
     For Each Cell In ChkRange 
     Dim Cel As Range 
      If InStr(1, Cell.Value, .Value) > 0 And Cell.Value <> .Value Then 
      'include the 2nd part of the "AND" to ensure you don't delete based 
      'on a COMPLETE match, only on a partial match as noted in the comments 
      Cell.EntireRow.Delete 
     End If 
     Next 
    End If 

    End With 
     Next Lrow 
      End With 
    ActiveWindow.View = ViewMode 
    With Application 
       .ScreenUpdating = True 
       .Calculation = CalcMode 
      End With 
     End Sub 
+0

菠萝是否算作像苹果公司? – pnuts 2015-04-02 14:44:08

+0

如果整个值存在于另一个字符串中,我不希望宏将其删除,因为这会删除批量值 – 2015-04-02 14:55:34

+0

因此,无论是在开始处还是在后面加上一个空格(或标点符号?),或者在空格前面和末尾(或只有标点符号)?不过呢,说'The'和'The Ltd.'。 – pnuts 2015-04-02 15:06:36

回答

0

如何使用InStr()原始删除一整行?修改代码

If Not IsError(.Value) Then 
    If Not IsError(Application.Match(.Value, ChkRange, 0)) Then 
    .EntireRow.Delete 
    End If 
End If 

的这部分内容是这样的:

Dim cel as range 'put this at the top of your code with your other Dim stmts 
If Not IsError(.Value) Then 
    If Not IsError(Application.Match(.Value, ChkRange, 0)) Then 
    .EntireRow.Delete 
    End If 
'note this changed row. Note carefully the SPELLING 
    for each Cel in ChkRange.Cells 
    if Instr(1, Cel.value, .Value) > 0 and ce.lValue <> .Value then 
     'include the 2nd part of the "AND" to ensure you don't delete based 
     'on a COMPLETE match, only on a partial match as noted in the comments 
     cel.EntireRow.Delete 
    end if 
    next 
End If 

这可能会比较慢,因为你必须通过每个细胞循环中ChkRange看看是否Sheet1.Cells(Lrow,"I").Value是在那里的某个地方。

注:这是我的头和未经考验的顶部,但应该工作,或至少让你关闭

+0

'如果 对于ChkRange中的每个单元格 If InStr(1,Cell.Value,.Value )> 0并且Cell.Value <> .Value然后 '包括“AND”的第二部分以确保您在完成匹配时不会删除基于 ',仅在注释中注明的部分匹配 Cell .EntireRow.Delete End If Next End If'我需要在这里打印哪个对象? – 2015-04-02 18:18:34

+0

@ThomasSharp'Dim Cel as Range'我相信应该这样做。如果不是这样,请将其添加到您的OP中,因为在评论中阅读非常困难。 – FreeMan 2015-04-02 18:35:49

+0

@ThomasSharp我看到你编辑了OP,但是我没有看到你把这段代码放在哪里。也许你现在粘贴你的代码,并把它放在另一个代码块的位置,这样我们可以再看一次 – FreeMan 2015-04-02 20:33:28