VBA检查search值是否是某种东西

我有一个macros来添加和删除客户端到数据库。 我希望能够停止,如果客户端存在于数据库中,并继续,如果没有。 如果客户端已经存在,以下工作:

Dim newClient As String, foundRange As Range newClient = Sheets("Summary").Range("K2").Value 'check if client exists Set foundRange = Sheets("Per Client").Cells.Find(what:=newClient, After:=ActiveCell, LookIn:=xlFormulas, lookat _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If foundRange = newClient Then MsgBox "Client " & newClient & " already exists." GoTo finishUp End If 'add new client 'code continues here' 

目前如果客户端确实存在,那么foundRange等于newClient ,用户得到消息,macros跳转到finishUp 。 但是,如果foundRange = Nothing ,我得到了

 Object variable or With block variable not set 

错误。

这是在macros中被foundRange的唯一时间。 什么是解决这个最好的办法,应该foundRange = Nothing

如果没有find单元格, foundRange的确等于Nothing

你可以检查如下:

If Not foundRange is Nothing Then MsgBox "Client " & newClient & " already exists." GoTo finishUp End If

好吧,我想出了If Not Nothing

 If Not foundRange Is Nothing Then MsgBox "Client " & newClient & " already exists." GoTo finishUp End If