目前我正在尝试扫描我的书中我写的if语句,如果该id不再存在,它会识别唯一ID然后返回“Not in Book”。
要清理我看到的“Not in Book”所有区域的书,我创建了一小段代码来清除所有这些条目。
Dim MWS As Worksheet
Set MWS = Sheets("Marks")
MWS.Cells.Find(what:="Not in Book", LookIn:=xlValues).EntireRow.Delete
此声明的适当方式是......
一个。扫描整本书中的这个短语并删除该行,而不是一次删除一行。
B中。要对此语句进行错误控制,以便当“Not in Book”不存在时,用户不会收到错误,而是什么都不会发生,或者会出现一个msgbox,说明该书是干净的。
谢谢,
答案 0 :(得分:2)
你可以这样做。将搜索分配给范围变量并检查它是否为无。
Sub x()
Dim rFind As Range, MWS As Worksheet
Set MWS = Sheets("Marks")
With MWS
Set rFind = .Cells.Find(what:="Not in Book", LookIn:=xlValues, _
Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
Do
rFind.EntireRow.Delete
Set rFind = .Cells.Find("Not in Book")
Loop Until rFind Is Nothing
Else
msgbox "Term not found"
End If
End With
End Sub
答案 1 :(得分:0)
我认为最简单的方法是遍历您的单元格并检查单元格值是否等于您的单词然后删除该行。这是代码。
Sub CleanTheSheet()
dim rng as range
for each rng in activesheet.usedrange.cells ' Here you can specify name of your sheet or the range if you have such a info
if rng.value ="Not in Book" then rng.entirerow.delete
next
if worksheetfunction.countif(activesheet.usedrange,"Not in Book") = 0 then msgbox "The Sheet is Clean Now!"
END SUB