Handle Error 9 when there is an Empty Array

后端 未结 8 1645
无人共我
无人共我 2021-01-02 17:43

I am writing a script that will loop through an Excel spreadsheet and find if there are duplicates of selected cells. If there are duplicates then the function will return

8条回答
  •  自闭症患者
    2021-01-02 18:24

    Your function is failing because if there is no error raised by UBound() (i.e. the array is dimensioned) then Err.Number is 0 and:

    Case 0
      IsArrayEmpty = True
    

    is executed returning an incorrect result.

    The simplest way is to just trap the error:

    Function IsArrayEmpty(anArray As Variant) As Boolean
    On Error GoTo IS_EMPTY
    If (UBound(anArray) >= 0) Then Exit Function
    IS_EMPTY:
        IsArrayEmpty = True
    End Function
    

提交回复
热议问题