I've been running a VBA code in Excel 2016 that loads a bunch of values into array from a single-column Excel table and then runs a FOR-loop on each value.
Sub Load_array()
Dim x As Long
Dim myArray() As Variant
Dim myTable As ListObject
Set myTable = Sheets("Sheet1").ListObjects("Table1")
TempArray = myTable.DataBodyRange.Columns(1)
myArray = Application.Transpose(TempArray)
For x = LBound(myArray) To UBound(myArray)
Range("A1").Value = myArray(x)
Next x
End Sub
Works fine up until I found a "bug". In case there is only a single row in that DataBodyRange, I'll get an error:
myArray = Application.Transpose(TempArray)
"Run time error '13': Type mismatch"
I could use a worksheet-based Excel COUNTA-formula and add IF-clause to re-route the code in case there is only a single value to be loaded, however perhaps there's a more elegant way to handle this? My experience on working with arrays is not extensive at all, so perhaps you guys could guide me a little?
Transposing a single-value array does not work like that, right?
Ifto take a separate route if there's only one cell.