如何在Excel中搜索单词使用VBA,然后删除整行? [英] How do I search for a word in Excel Using VBA and then Delete the entire row?

查看:149
本文介绍了如何在Excel中搜索单词使用VBA,然后删除整行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请帮忙。
我试图编写一个VBA代码,在我的excel工作表列D中搜索一个特定的单词DR,然后删除整个行。
工作表中有很多特定的单词出现。我想要做的是搜索这些事件,然后删除包含这些单词的整个行。
我的问题是,我不知道要使用什么循环结构。
以下是我正在使用的代码。

 
列(D:D)。选择
Cells.Find(什么:=DR,之后:= ActiveCell,LookIn:= xlFormulas,LookAt := _
xlPart,SearchOrder:= xlByRows,SearchDirection:= xlNext,MatchCase:= False _
,SearchFormat:= False).Activate

Do
单元格。 Find(What:=DR,After:= ActiveCell,LookIn:= xlFormulas,LookAt:= _
xlPart,SearchOrder:= xlByRows,SearchDirection:= xlNext,MatchCase:= False _
,SearchFormat := False).Activate



ActiveCell.EntireRow.Delete

循环while(Cells.Find(什么:=DR ))



我会很高兴获得帮助。

解决方案

清洁简单,做的窍门! ;)

  LastRow = Cells(Rows.Count,D)。End(xlUp).Row 

对于i = LastRow到1 Step -1
如果Range(D& i).Value =DRthen
Range(D& i).EntireRow.Delete
结束如果
下一个我


Someone please help. I'm trying to write a VBA code that searches for a particular word "DR" in my excel worksheet column "D" and then delete the entire row. There are lots of occurrences of the particular word in the worksheet. All I want to do is to search for the these occurrences and then delete the entire rows that contains those words. My problem is that I'm not sure what loop structure to use. Below is the code I'm using.

    Columns("D:D").Select
    Cells.Find(What:="DR", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
Do Cells.Find(What:="DR", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ , SearchFormat:=False).Activate

ActiveCell.EntireRow.Delete Loop While (Cells.Find(What:="DR"))

I'll be glad for an assistance.

解决方案

Clean and simple, does the trick! ;)

LastRow = Cells(Rows.Count, "D").End(xlUp).Row

For i = LastRow To 1 Step -1
   If Range("D" & i).Value = "DR" Then
      Range("D" & i).EntireRow.Delete
   End If
Next i

这篇关于如何在Excel中搜索单词使用VBA,然后删除整行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆