View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Alan Beban[_2_] Alan Beban[_2_] is offline
external usenet poster
 
Posts: 783
Default Extracting sub arrays from a 2-D VBA array

Harlan Grove wrote:

"Alan Beban" wrote...
...


It arose out of exploring ways to return non-contiguous columns from a
VBA array.



Colapsing 2D VBA arrays by removing unwanted columns? The following would
seem simpler, and is almost certainly faster than repeated calls to anything
like your proc. . . .

Well, it might certainly be faster than even a single call to a
procedure, but if you're willing to hardcode the columns to be
extracted, as you do with wc, it doesn't require *repeated* calls; e.g.,

Function SubArrayFormula3(InputArray)
numRows = UBound(InputArray) - LBound(InputArray) + 1
numCols = UBound(InputArray, 2) - LBound(InputArray, 2) + 1
Worksheets.Add
ActiveSheet.Name = "xyz1"
Set rng1 = Range("a1").Resize(numRows, numCols)
rng1.Value = InputArray
Worksheets.Add
ActiveSheet.Name = "xyz2"
Set rng2 = Range("A1").Resize(numRows, 3)
rng2.FormulaArray = "=INDEX(xyz1!" & rng1.Address & ",ROW(1:" &
numRows & "),{2,3,5})"
SubArrayFormula3 = rng2.Value
Application.DisplayAlerts = False
Sheets("xyz1").Delete
Sheets("xyz2").Delete
Application.DisplayAlerts = True
End Function
'assumes 2D array a already exists, and desired
'columns from a specified in 1D array wc
'
Dim wc As Variant, b As Variant
Dim i As Long, j As Long, n As Long

wc = Array(1, 3, 5, 7)
n = UBound(wc, 1)

ReDim b(LBound(a, 1) To UBound(a, 1), 1 To n + 1)

For i = LBound(a, 1) To UBound(a, 1)
For j = 0 To n
b(i, j + 1) = a(i, wc(j))
Next j
Next i

Alan Beban