0

There are definitely easier ways to achieve what this code is trying to do, but I created a simplified code to show what I am trying to achieve. Basically I want to perform calculations on lots of data and I don't want to have to have to add that to the worksheets.

So basically I need to run formulas like sum, averageifs, etc. in a worksheet through using an Array that I defined in my Macro. At this point I can't figure out how to turn this array, containing 5 values, into a "=sum( )" function. Thanks in advance for your help!

Sub ArraySum()

Dim numbers(5) As Double
Dim E As Double

For I = 1 To 5
    E = Cells(I, 1)
    numbers(I) = Cos(E)
Next I

Range("b1").Formula = "=sum(" & numbers & ")"

End Sub
0

3 Answers 3

2

It's not clear why you'd want to do it this way, but:

Sub ArraySum()

    Dim numbers(1 To 5) '<<<
    Dim E As Double

    For I = 1 To 5
        E = Cells(I, 1)
        numbers(I) = Cos(E)
    Next I

    Range("B1").Formula = "=SUM(" & Join(numbers, ",") & ")"

End Sub

but then you might as well just do:

Range("B1").FormulaArray = "=SUM(COS(A1:A5))"
Sign up to request clarification or add additional context in comments.

5 Comments

Ya it does seem unclear in my mind as well. My end goal would be achieved if I could just do something like this formula within excel, is there a way to do this? =averageif(cos(i2:i5), ">0", cos(i2:i5))
What exactly do you mean by "within excel" ? - on a worksheet you can just use the array formula in my second example (use Ctrl+Shift+Enter when entering it on the sheet)
Ya I mean on a worksheet. Right, I can do your example with the ctrl+shift+enter, but it's not letting me do it for averageif(cos(a1:a5), ">0"). Do you know what the problem could be? Appreciate your help.
Not sure you can use an array formula with averageif
Can fake it with something like =SUM((COS(A1:A5)>0)*COS(A1:A5))/SUM((COS(A1:A5)>0)*1)
0

Does it have to be an equation in the worksheet? You could just get the total and put it there:

Sub ArraySum()

    Dim numbers(1 To 5) '<<<
    Dim E As Double

    For I = 1 To 5
        E = Cells(I, 1)
        numbers(I) = Cos(E)
    Next I

    Range("B1").Value = Application.WorksheetFunction.Sum(numbers)
End Sub

Comments

0

Another way if you don't need the formula to actually be in that cell:

Range("b1").Value = Application.WorksheetFunction.Sum(numbers)

Edit: If you haven't set Option Base 1 then Dim numbers(3) As Double will create a 4-element array numbers(0),...,numbers(3) since the default is Base 0. I would recommend using Dim numbers(1 to 3) As Double rather than relying on the Base option.

Edit 2: You either have forgotten Dim I in your example or don't use Option Explicit which is highly reccomended!

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.