1

If I have defined named ranges for some blocks - say for example, 2 by 2 blocks named Apple1, Apple2, Apple3, Apple4

and I want to create a Total Block being the sum of the above (in excel, the formula array formula would be {Apple1 + Apple2 + Apple3 + Apple4} With the FormulaArray command - am I on the right lines in my thought process below?

For i = 1 to 2
range("Total").formulaarray = "=" & "Apple" & i
next i

2 Answers 2

1

I've not understand why you need FormulaArray for such task, but you can use this:

Sub test()
    Dim i%, Apples$
    For i = 1 To 4
        Apples = Apples & "+SUM(Apple" & i & ")"
    Next i
    [Total].FormulaArray = "=" & Mid(Apples, 2, Len(Apples))
End Sub

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

thank you, i will give this a go. Note, the formulaarray is because i want the "Total" to be an 2x2 array as well.
@psw very valuable comment, I've understand your point ({=SUM(a+b+c)}), but answer has been posted to show the way of how to put values to array formula, not for give a best possible way to solve the issue of the author. have no idea what behind the OP needs.
@Vasily, I do not understand your point. Since you use Sum function, why do you use a+b+c? If a+b+c is used, Sum is redundant.
@pwc {=SUM(a+b+c)} where a,b,c named ranges, e.g. {=SUM(Apple1+Apple2+Apple3)}
@Vasily, Why not use {=a+b+c}? It is brevity.
|
1

After my experiment, I find the behavior of FormulaArray is more complicated.

sub test()
    dim i as long, Apples1 as variant, Apples2 as variant

    for i = 1 to 3
        Apples1 = Apples1 & "Apple" & i & "+"
        Apples2 = Apples2 & "Apple" & i & ","
    next i

    range("total1").FormulaArray = "=sum(" & left(Apples1, len(Apples1)-1) & ")"
    range("total2").FormulaArray = "=sum(" & Apples2 & ")"
    range("total3").FormulaArray = "=" & left(Apples1, len(Apples1)-1)
end sub

the outcome is enter image description here

If any cells in Apple1, Apple2 or Apple3 are string, the outcome is enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.