Solved

Insert Sumif(s) formula with VBA

Posted on 2014-10-06
3
5,833 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

628 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