Solved

Insert Sumif(s) formula with VBA

Posted on 2014-10-06
3
5,463 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

734 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