Solved

Excel Formula writing with VBA

Posted on 2012-09-07
865 Views
Last Modified: 2012-09-07
So lets say we have 10 rows of data on excel

-4
2
4
-5
1
5
-6
7
-9
2

On the 11th row I would like to write a formula (not just the value) which adds the positive value in the 10 rows above.
On the 12th row I woul like to write a formula which adds the negative value in the 10 rows above.

So something similar to

Sheets(a.Name).Range("T2").Offset(isbet + 3, 0).Value = "=Sum(T" & 2 & ":" & "T" & isbet + 1 & ")"

but instead of sum function it is supposed to be =E2+E5+E7....

The values in that 10 row is going to change everyday.
0
Question by:awesomejohn19
3 Comments

LVL 9

Accepted Solution

This should be exactly what you need mate:
Sum Only Negative Numbers
0

LVL 17

Expert Comment

``````=SUM(IF(\$T\$2:\$T\$11>0,\$T\$2:\$T\$11,0))
=SUM(IF(\$T\$2:\$T\$11<0,\$T\$2:\$T\$11,0))
``````
Confirmed with ctrl-shift-enter as an array formula.

Are the formulas you need.  Do you want to enter these with VBA?  If so you need to use the FormulaArray  property.
0

LVL 9

Expert Comment

I do love array formulas but there is no need for that here this will do it:
=SUMIF(T2:T10,”<0¿)

or in VBA
Dim SumRange As Range
Set SumRange = Sheets(1).Range("T1")

With SumRange
.Formula = "=SUMIF(T2:T10,"<0",T2:T10)"
.Value = .Value
End With
0

Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

Featured Post

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

779 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!