Solved

Insert Sumif(s) formula with VBA

Posted on 2014-10-06
3
5,214 Views
Last Modified: 2014-10-06
Hi,

Please help. Trying to insert these formulas into a Cell via VBA has proven to be a real nightmare.

 Range("M20").Select
 ActiveCell.FormulaR1C1 = "=SUMIF('Sheet1'!F:F," = "&TODAY()-10,'Sheet1'!K:K)"

Range("M21").Select
 ActiveCell.FormulaR1C1 = "=SUMIFS('Sheet1'!K:K,'Sheet1'!F:F,"<"&TODAY()-10,'Sheet1'!F:F,">"&TODAY()-20)"

Thanks in advance,

swjtx99
0
Comment
Question by:swjtx99
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40364853
You just need to use the .Formula property and not .FormulaR1C1 (relative references).  Here are your updated formulas:
Range("M20").Formula = "=SUMIF(Sheet1!F:F,TODAY()-10,Sheet1!K:K)"
Range("M21").Formula = "=SUMIFS(Sheet1!K:K,Sheet1!F:F,""< ""&TODAY()-10,Sheet1!F:F,""> ""&TODAY()-20)"

Open in new window

Regards,
-Glenn
0
 

Author Closing Comment

by:swjtx99
ID: 40364866
Hi Glenn,

Thanks! Not sure why R1C1 does not work. I also had to add double quotes as below:

    Range("M20").Formula = "=SUMIF('Sheet1'!F:F,""=""&TODAY()-10,'Sheet1'!K:K)"
    Range("M21").Formula = "=SUMIFS('Sheet1'!K:K,'Sheet1'!F:F,""<""&TODAY()-10,'Sheet1'!F:F,"">""&TODAY()-20)"

Thanks for your help!

swjtx99
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40364879
The .FormulaR1C1 property requires that you specify relative references - in rows and column shift - to the cell in which the formula is entered.  

If you wanted to use that property for your formulas they would look like this instead:
Range("M20").FormulaR1C1 = "=SUMIF(Sheet1!C[-7],TODAY()-10,Sheet1!C[-2])"
Range("M21").FormulaR1C1 = "=SUMIFS(Sheet1!C[-2],Sheet1!C[-7],""<""&TODAY()-10,Sheet1!C[-7],"">""&TODAY()-20)"

Open in new window


-Glenn
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

685 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