2016-11-17 65 views
0

我有一个超过10,000行的excel文件。我想运行一个VBA脚本,删除所有列B结束单词reduce中的行。例如,如果我的专栏是这样的:如果一个单元格以某个单词结尾,我该如何删除整行? VBA

CostReduce 
PriceReduce 
ReducePrice 
MaterialReduce 
InfrastructureReduce 
ReduceProfits 
ReduceOverhead 

我想脚本运行,删除具有在Reduce结尾的单词中的每一行。因此,输出将是:

ReducePrice 
ReduceProfits 
ReduceOverhead 

,我现在所拥有的脚本删除包含单词减少所有行,我不知道我该怎么去,因此我想要做什么改变它。

Sub DeleteReduce() 

Dim ContainWord As String 

Dim i As Integer 
i = 2 

ContainWord = "reduce" 

Do While Range("B" & i) <> "" 
    If Not Range("B" & i).Find(ContainWord) Is Nothing Then 
     Range("B" & i).EntireRow.Delete 
    Else 
     i = i + 1 
    End If 
Loop 
Range("B2").Select 
End Sub 
+0

代替你的指令'如果没有范围( “B” &I).Find(ContainWord)是没有什么Then'通过'如果范围( “B” &I),如 “*” 和containword then' – h2so4

回答

1

使用Right功能,小的改动你的VB:

Sub DeleteReduce() 

Dim ContainWord As String 

Dim i As Integer 
i = 2 

ContainWord = UCase("reduce") 

Do While Range("B" & i) <> "" 
    If UCASE(right(Range("B" & i).value,len(ContainWord))) = ContainWord Then 
     Range("B" & i).EntireRow.Delete 
    Else 
     i = i + 1 
    End If 
Loop 
Range("B2").Select 
End Sub 

更新,删除区分大小写

+0

我喜欢这个解决方案,并且可以看到它背后的逻辑,但是它会在这一行上抛出一个错误:'如果不正确(Range(“B”&i),Len(ContainWord))= ContainWord Is Nothing Then' – Abtra16

+0

说:“运行时错误'424':对象需要” – Abtra16

+0

@ Abtra16请检查现在,我删除了'没有'的部分,这是不再有用。也使它不敏感 – EoinS

0

让它检查单元格中的最后6个字符,看它们是否匹配Reduce。

Right(Range("B" & i),6) = "Reduce"

Sub DeleteReduce() 
Dim ContainWord As String 
Dim i As Integer 

    ContainWord = "Reduce" 

    Do While Range("B" & i) <> "" 
    If Right(Range("B" & i),6) = ContainWord Then 
     Range("B" & i).EntireRow.Delete 
    Else 
     i = i + 1 
    End If 
    Loop 
    Range("B2").Select 
End Sub 
+0

这没有工作,没有被删除。 – Abtra16

+1

尝试将“减少”更改为“减少” – Rdster

+0

有点工作。 'CostReduce'没有被删除。 – Abtra16

1

你真的需要一个脚本?用简单= IF(RIGHT(B1,6)=“reduce”,“yes”,“no”)引入另一列并应用一个过滤器,然后删除具有“yes”值的行是不够的吗?

+0

我很感激!但是我想用VBA脚本来做。 – Abtra16

+0

今天没用了 – Eleshar

0

该解决方案使用Autofilter设置与Rows一个Range被删除,然后介绍了两种方法来删除行:

  1. 删除整个范围内的一次:但是这取决于量,这可能是缓慢的区域,文件大小等。
  2. 以升序显示区域的删除结果(从下到上)。

这两种方法在下面的代码中都是“活动的”,您需要评论没有选择的方法。

Sub Rows_Delete_EndingWord_Published() 
Dim sCriteria As String 
sCriteria = "Reduce" 'Change as required 
Dim rDta As Range, rTmp As Range 
Dim l As Long 

    Application.Calculate 
    Application.EnableEvents = False 
    Application.Calculation = xlCalculationManual 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    Rem Set Data Range 
    With ThisWorkbook.Sheets("Sht(0)") 'Change as required 
     If Not (.AutoFilter Is Nothing) Then .Cells(1).AutoFilter 
     Set rDta = Range(.Cells(1, 2), .Cells(1, 2).End(xlDown)) 
    End With 

    Rem Filter Data Range & Set Resulting Range 
    With rDta 
     Set rTmp = .Offset(1, 0).Resize(-1 + .Rows.Count, 1) 
     .AutoFilter Field:=1, Criteria1:="=*" & sCriteria 
     On Error Resume Next 
     Set rTmp = rTmp.SpecialCells(xlCellTypeVisible) 
     On Error GoTo 0 
     .AutoFilter 
    End With 

    Rem Delete Filtered Data 
    Rem Presenting two methods - need to uncomment the method chosen 
    If Not (rTmp Is Nothing) Then 

     Rem Method 1 - Deleting entire range at once 
     Rem However it could be slow depending on the quantity of areas, size of the file, etc. 
     rTmp.EntireRow.Delete 

     Rem Method 2 - Deleting the range by Area in Ascending Order (Bottom to Top) 
     For l = rTmp.Areas.Count To 1 Step -1 
      rTmp.Areas(l).EntireRow.Delete 
     Next 

    End If 

    Application.Calculation = xlCalculationManual 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 
    Application.EnableEvents = False 

End Sub 
+0

这太复杂了! – Eleshar

+0

也许,但不要害怕学习。特别是在涉及半大型和大型数据时,它对物体的处理效率很高。此代码选择所需的数据并立即删除它们,而不是运行无限循环。 – EEM

相关问题