View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Harlan Grove Harlan Grove is offline
external usenet poster
 
Posts: 733
Default Extracting sub arrays from a 2-D VBA array

"Alan Beban" wrote...
....
. . . no add-in required. . . .


Perhaps not, but VBA required.

. . . It
involves transferring the array to a worksheet, extracting the sub array
to a second worksheet by means of an array formula, and then writing the
sub array range to a VBA array. I have run *no* tests to explore speed
of execution.

Sub SubArrayFormula(InputArray, row1, row2, col1, col2)
Dim rng1 As Range, rng2 As Range,icol1 As Long, icol2 As Long
Dim MySubArray As Variant
Worksheets.Add
ActiveSheet.Name = "xyz1"

....

So you're unfamiliar with storing the result of the Add method, which is
just a function call returning a worksheet object, in a variable?

Dim ws As Worksheet
Set ws = Worksheets.Add

Then you wouldn't need to bother naming that worksheet to keep track of it.

Select Case col1
Case Is < 27
icol1 = Chr(64 + col1)

....

This is *MUCH* easier when you use R1C1 addressing.

rng2.FormulaArray = "=INDIRECT(""xyz1!R" & row1 & "C" & col1 _
& ":R" & row2 & "C" & col2 & """,0)"

MySubArray = rng2.Value
Application.DisplayAlerts = False
Sheets("xyz1").Delete
Sheets("xyz2").Delete
Application.DisplayAlerts = True
End Sub


And your reason for not assigning this *directly* to MySubArray using
Evaluate is what, precisely?

MySubArray = Evaluate("=INDIRECT(""xyz1!R" & row1 & "C" & col1 _
& ":R" & row2 & "C" & col2 & """,0)")

This would eliminate the 'need' for the second worksheet.

Also, your proc lack any means of passing MySubArray back to the calling
proc, so does nothing other than waste cycles. So it's purpose was what,
precisely?

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.

'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


And colapsing 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