View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone Jim Cone is offline
external usenet poster
 
Posts: 3,290
Default Array Formulas in VBA

Bill,
Did you enter data in the two ranges on the active sheet?
Is the code in a standard module not a module behind a sheet?
Jim Cone

"Bill Martin" wrote in message ...
I can't get your example to run Jim. When I copy/paste it directly into a
module and try to execute it, the code compiles properly, but execution will
stop with a "Type Mismatch" error at the line:

lngC = UBOUND(varArray,2)

I tried touching various things, but any time the varArray is in the right
hand of a formula I get the same error.

Bill
--------------------------------------------------------------
"Jim Cone" wrote in message
...
Bill,
Maybe you can use some of this...
Note that each variant contains an array.
'---
Sub MatrixNumbers()
Dim varArray As Variant
Dim varCol As Variant
Dim varRow As Variant
Dim lngC As Long
Dim lngR As Long

varArray = Application.MMult(Range("B5:B9"), Range("C5:E5"))
lngC = UBound(varArray, 2)
lngR = UBound(varArray, 1)

'Place on worksheet if desired
'ActiveCell.Resize(lngR, lngC).Value = varArray

'Get second column
varCol = Application.Index(varArray, 0, 2)
'Place on worksheet if desired
'ActiveCell.Resize(lngR).Value = varCol

'Get third row
varRow = Application.Index(varArray, 3, 0)
'Place on worksheet if desired
'ActiveCell.Offset(0, 1).Resize(, lngC).Value = varRow
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Bill Martin" wrote in message
...
Is there some way to use an array formula within VBA? For example, I find
that I can use a MMULT array formula in VBA with the result posted to a
spreadsheet by using:

Range("AD6:AD105").Select
Selection.FormulaArray = "=MMULT(D6:D105,TRANSPOSE(Sheet2!E3:P3))"

But it would be much better for me if I could put the resulting vector
directly into a VBA array for use rather than back out onto the
spreadsheet.

I can do this with scalar functions that take an array input and produce a
single result such as:

dim Vector(10) as single
... fill the vector ...
X = WorksheetFunction.Max(Vector)

I haven't been able to figure out how to do the analogous thing with built
in worksheet array functions though.
Thanks.
Bill