VBA:需要确定 FIND 方法的结果是 NOTHING 还是 ""(空)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14707731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:28:36  来源:igfitidea点击:

VBA: need decide if result of FIND method is NOTHING or "" (empty)

excelvbaexcel-vba

提问by koubin

I have this

我有这个

Set rngFound = Selection.Find(What:=Trim(prirustek.Cells(i, 1).Value), LookIn:=xlValues, LookAt:=xlWhole)

and need to do somethng like this:

并且需要做这样的事情:

If rngFound Is Nothing Or rngFound = "" Then
...

but this code stops with "Object variable or With block variable not set (Error 91)". I think this problem is in rngFound that is Nothing, but how can I make

但此代码以“未设置对象变量或块变量(错误 91)”而停止。我认为这个问题在 rngFound 中是什么,但我怎样才能做到

rngFound = "" 

in IF statement?

在 IF 语句中?

采纳答案by Brad

Try this one If IIf(rng Is Nothing, "", rng) = "" Then

试试这个 If IIf(rng Is Nothing, "", rng) = "" Then

It deals with the whole nothingsituation first. This is kind of like Nz()in Access.

它首先处理整个nothing情况。这有点像Nz()Access。

回答by Peter L.

Try If rngFound Is Nothing Or rngFound.Value = "" Then

尝试 If rngFound Is Nothing Or rngFound.Value = "" Then

回答by assylias

How about:

怎么样:

Private Function isRangeEmptyOrNothing(r As Range) As Boolean

  If r Is Nothing Then
    isRangeEmptyOrNothing = True
  ElseIf IsEmpty(r) Then
    isRangeEmptyOrNothing = True
  ElseIf r = "" Then 'Not sure if this is redundant with IsEmpty
    isRangeEmptyOrNothing = True
  End If

End Function

Then in your main code:

然后在你的主代码中:

If isRangeEmptyOrNothing(rngFound) Then

回答by Pellizon

You can try the following:

您可以尝试以下操作:

If rngFound = Empty Then ...

or

或者

If isNull(rngFound) = true Then ...