Search code examples
arraysexcelvbavariant

Single Dimension Variant Arrays VBA


In general I have a good macro for change management for a single value and now can write one for an multi dimensional array but need to be able to differential between without the use of error handling.

Is there any other work around for when the target is only one cell? the error handling below handles the issue but I consider it to be "sloppy." Suggestions are appreciated on a better method.

Sub Dims(target As Variant)

Dim varData As Variant
Dim i As Integer
Dim j As Integer

varData = target

On Error GoTo Err

For i = 1 To UBound(varData, 1)
    For j = 1 To UBound(varData, 2)
        Debug.Print i, j, varData(i, j)
    Next j
Next i

Err:
If Err.Number = 13 Then
    Debug.Print target.Value
ElseIf Err.Number <> 0 Then
    MsgBox "Error " & Err.Number & " just occured."
ElseIf Err.Number <> 13 And Err.Number <> 0 Then
    Debug.Print "Err No.= "; Err.Number
Else
    Debug.Print "No Error"
End If
End Sub

Solution

  • I set up a if then statement to run one way if target.count =1 and another if target.count>1