2

我有这个

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

并且需要做这样的事情:

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

但此代码以“对象变量或未设置块变量(错误 91)”停止。我认为这个问题出在 rngFound 中,它什么都没有,但我该怎么做

rngFound = "" 

在 IF 语句中?

4

4 回答 4

2

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

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

于 2013-02-05T13:10:00.400 回答
0

怎么样:

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

然后在您的主代码中:

If isRangeEmptyOrNothing(rngFound) Then
于 2013-02-05T12:46:17.673 回答
0

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

于 2013-02-05T12:40:51.013 回答
0

您可以尝试以下方法:

If rngFound = Empty Then ...

或者

If isNull(rngFound) = true Then ...
于 2013-02-05T12:48:46.733 回答