I found this worked well for an Excel Range, where the result would be array(n) notation instead of array(1, n)
Result = Application.Transpose(Application.Transpose(Worksheets(kSheet).Range("Y20:AC20")))
However I have a result from .getrows which is array (n,0) notation. Can this be converted to arry(n) notation similar to above ?
You have discovered an anomaly (bug?) and, with a very limited explanation of that anomaly, ask how to extend its availability. That is why no one can understand your question.
Explanation of the anomaly
If you load a single cell into a variable of type Variant that Variant will hold a single value with its type defined by the cell.
If you load a column into a variable of type Variant that Variant will hold an array with dimensions
(1 To NumRows, 1 To 1)
with each element having its type defined by the corresponding cell.If you load a row into a variable of type Variant that Variant will hold an array with dimensions
(1 To 1, 1 To NumCols)
.If you load a rectangle into a variable of type Variant that Variant will hold an array with dimensions
(1 To NumRows, 1 To NumCols)
.If you do not like the sequence of the dimensions, you can use
WorksheetFunction.Transpose
to swap them.If you use
WorksheetFunction.Transpose
twice, I would expect the array would be restored to its original state. I cannot find any documentation that suggests otherwise.You have discovered that if you load a row and then use
WorksheetFunction.Transpose
twice, the first dimension is removed. That is, the dimensions are changed from(1 To 1, 1 To NumCols)
to(1 To NumCols)
.However, if you load a column and then use
WorksheetFunction.Transpose
twice, the dimensions are restored to their original state.My solution
I consider the effect of
WorksheetFunction.Transpose
on a row to be a bug. The problem with relying on a bug is that it might be fixed in a future version of Excel or might not exist in an earlier version.Another problem I have discovered recently is that some, perhaps all, worksheet functions are slow. I doubt they are slow when used within a worksheet formula so assume this is an overhead with the call from VBA.
The macro
Timings
, below, demonstrates this effect. The timings are from my 2.1 GHz laptop; your timings may be different but I would expect the relationships to be unchanged. Note also that the timings I show are averages from 10 runs of the macro.I have filled “A1:T10000” of worksheet “Sheet1” with values. The macro
Timings
loads data from the worksheet and manipulates to get these timings:I do not know why transposing one way is quicker than transposing the other way. However, you can see that
WorksheetFunction.Transpose
takes three times as long as my VBA routine. If you are only loading one range, this is unimportant. However, if you are loading many ranges that extra time will become important.The second set of rows shows the effect of loading a column and transposing it twice and the effect of loading a row and transposing it twice. The final row shows the anomaly you discovered: the first dimension has been removed by the double transpose.
The macro
Test
demonstrates the use of functionRemoveUpperEqLowerDim
. You ask how to extend the use of the anomaly; I do not believe this is possible. FunctionRemoveUpperEqLowerDim
may use looping but it is quicker thatWorksheetFunction.Transpose
and works with both row and column ranges.