In the OP code the nested loops fail when the range evaluates to a single cell. In this manner Excel is performing according to its specification. That doesn't stop the error from being annoying.
In my previous comments I make the observation that Excel does not behave orthogonally in that if a range evaluates to a single row or column you actually get a 2d array i.e one of the dimensions is scoped as 1 to 1.
The code below attempts to reconcile the above observations by allowing a range to return a 2D array even if the range evaluates to a single cell. Additionally, the optional parameter allows for a value to be returned that represents the true size of the range, a single cell returns a single value, a single row or column returns a 1 dimensional array etc.
It should be noted that when the optional variable is False, and a single dimensioned array is returned, you lose the orientation of the original range.
In the test below, the nested loop runs correctly even though the range specifies a single cell.
Public Sub ttest()
Dim myVar As Variant
myVar = GetRange(ThisWorkbook.Sheets.Item(1).Range("A1:A1"))
Dim myRow As Long
For myRow = LBound(myVar, 1) To UBound(myVar, 1)
Dim myCOl As Long
For myCOl = LBound(myVar, 2) To UBound(myVar, 2)
Debug.Print "Row "; myRow, "Col "; myCOl, " is ", myVar(myRow, myCOl)
Next
Next
End Sub
Public Function GetRange(ByVal ipRange As Excel.Range, Optional ByVal RangeAsTable As Boolean = True) As Variant
If RangeAsTable Then
GetRange = GetRangeAsTable(ipRange)
Else
GetRange = GetRangeAsSized(ipRange)
End If
End Function
Private Function GetRangeAsTable(ByVal ipRange As Excel.Range) As Variant
Dim myArray As Variant
If ipRange Is Nothing Then
ReDim myArray(1 To 1, 1 To 1)
GetRangeAsTable = myArray
Exit Function
End If
Dim xlArray As Variant
xlArray = ipRange.Value
If Not IsArray(xlArray) Then
ReDim myArray(1 To 1, 1 To 1)
myArray(1, 1) = xlArray
GetRangeAsTable = myArray
Exit Function
End If
GetRangeAsTable = xlArray
End Function
Private Function GetRangeAsSized(ByVal ipRange As Excel.Range) As Variant
Dim xlArray As Variant
xlArray = ipRange.Value
If Not IsArray(xlArray) Then
GetRangeAsSized = xlArray
Exit Function
End If
If UBound(xlArray, 1) = 1 Or UBound(xlArray, 2) = 1 Then
GetRangeAsSized = Application.WorksheetFunction.Transpose(xlArray)
Exit Function
End If
GetRangeAsSized = xlArray
End Function
Probably not the neatest or most efficient code but at least it demonstrates the point, and, more importantly, allows the OP to use a nested loop even if the range is a single cell.