View Single Post
  #5   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:

. . .
And collapsing arbitrary partial 2D arrays,

Dim wc As Variant, wr As Variant, b As Variant
Dim i As Long, j As Long, m As Long, n As Long

wr = Array(2, 4, 6, 8, 10, 11, 12)
m = UBound(wr, 1)
wc = Array(1, 3, 5, 7)
n = UBound(wc, 1)

ReDim b(1 To m + 1, 1 To n + 1)

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


Similarly, with a single call (again, given the hardcoding):

Function SubArrayFormula4(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(7, 4)
rng2.FormulaArray = "=INDEX(xyz1!" & rng1.Address & _
",{2;4;6;8;10;11;12},{1,3,5,7})"
SubArrayFormula4 = rng2.Value
Application.DisplayAlerts = False
Sheets("xyz1").Delete
Sheets("xyz2").Delete
Application.DisplayAlerts = True
End Function

Alan Beban