1

In userform1, I have the following code

Private Sub cmdOK_Click()
    Dim i As Long
    With Me.ListBox2
        If .ListCount = 0 Then MsgBox "You Have To Select At Least One Column", vbExclamation: GoTo Skipper
        ReDim aCols(0 To .ListCount - 1)
        For i = 0 To .ListCount - 1
            aCols(i) = "[" & ListBox2.List(i, 0) & "]"
        Next i
    End With
Skipper:
    Unload Me
End Sub

and in standard module I declared aCols as public

Public aCols

if listbox2 has no items then aCols became Empty while if there are items the aCols became an array .. Then in another code I am confused of how to avoid errors

If UBound(aCols) > -1 Then

This works fine if aCols is not empty but I encountered errors if aCols is Empty .. How to deal with both cases Simply I need to avoid the errors and deal with aCols either it is empty or either it is an array.

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95

1 Answers1

1

I would use Function safeUBound() which looks ugly due to OERN but works fine:

Function safeUBound(a)
    
    safeUBound = -1
    On Error Resume Next
    safeUBound = UBound(a)
    
End Function

Another solution is to assign empty array or empty 2d array to the variable aCols either at the very beginning of the code or at userform initialize.

omegastripes
  • 12,351
  • 4
  • 45
  • 96