MeSS83
MeSS83

Reputation: 459

Adding two 2D array in Excel VBA

I was just wondering how I can add two 2D arrays using VBA in Excel.

I tried to search on internet and it seems to me that the only way is to loop on each element. Am I right or is there any better/faster method?

As a corollary question: it would be better to loop or to copy to Range and use PasteSpecial options as in the spirit of the answer to the question how to add arrays ?

Upvotes: 0

Views: 1145

Answers (2)

lori_m
lori_m

Reputation: 5567

You can try the following Worksheetfunction method to add two VBA variant arrays:

Function ArrayAdd(A, B)
    ArrayAdd = Application.Pmt(, -1, A, B)
End Function

A and B can be arrays, ranges or values. ( https://stackoverflow.com/a/25596621/1252820)

Upvotes: 1

pnuts
pnuts

Reputation: 59475

Since there is considerable ambiguity about "adding arrays" assuming each is comprised of numbers only and is 3x3 in A1:C3 and A6:A8 then to output to J1:L3 please try:

Sub ArraySum()
Range("J1:L3").FormulaArray = "=RC[-9]:R[2]C[-7]+R[5]C[-9]:R[7]C[-7]"
End Sub

Upvotes: 0

Related Questions