Excel Formula writing with VBA

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


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.
Question by:awesomejohn19
    LVL 9

    Accepted Solution

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

    Expert Comment


    Open in new window

    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.
    LVL 9

    Expert Comment

    I do love array formulas but there is no need for that here this will do it:

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    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.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now