Application.Evaluate Does not work with SUMIF with ">" or "<" condition - excel

I'm trying to use Application.Evaluate method for evaluating a Sumif formula in which the criteria is ">="
Following is the line of code I'm trying to get an evaluation of
So I have Order Numbers 1 to 15 in Cell E1 to E15, and their Respective Amounts in Cell F1 to F15.
In J1 the user inputs his Order Number. Amount over and above that order number will be totaled and displayed using Sumif Function.
Now I want to find this answer using Application.Evaluate
MsgBox( Application.Evaluate("=SUMIF(E1:E15, ">=" & J1, F1:F15)"))
I get greeted with True or False Message Box.
Now I am guessing the inverted commas in Sumif Function ie. ">=" is causing this problem. Hence to fix this, I amended the function to
MsgBox( Application.Evaluate("=SUMIF(E1:E15, """">="""" & J1, F1:F15)"))
However now it returns 0, although there are values in it.
I would really appreciate if someone could help me understand a way around it
*Note: Please do not suggest to use any other function. I want to know it purely from Application.Evaluate perspective, as my further line of code depends on it. *
Thanks.

All quotes inside the string must be doubled:
">="
Should be
"">=""
Also:
Application.Evaluate
will work on the active sheet, thus if the wrong sheet is active the sumifs will be done on the wrong sheet since no sheet names are provided in the formula itself.
Use
Worksheets("Sheet1").Evaluate
Changing "Sheet1" to your sheet.
So in total:
MsgBox Worksheets("Sheet1").Evaluate("=SUMIF(E1:E15, "">="" & J1, F1:F15)")

Related

Insert Sum(sumif in VBA for multiple criteria, using formulaR1C1

Cannot get SUM(SUMIF to work with VBA, yields “Run-time error 1004”. SUM(SUMIF is typically used in Excel to sum using multiple criteria.
This is my formula.
Worksheets("units").Cells(i, j).FormulaR1C1 = "=pws!R[0]C[0]+SUM(SUMIF(pws!R1C3:R1C" & (2 + numlxp) & ",central!C[1]R3:C[1]R" & (2 + numatt) & ",pws!R[0]C3:R[0]C" & (2 + numlxp) & "))"
I also tried a version using OFFSETS instead of R1C1. The OFFSETS versions works within Excel itself, but not in VBA.
I then tried simplifying to the below, to see if there was an issue with SUM(SUMIF specifically within VBA, but it returns a formula with # for implicit intersectionality and gives the wrong result ‘0’:
Worksheets("units").Cells(2, 2).Formula = "=SUM(SUMIF(pws!C1:L1,central!C3:C6,pws!C2:L2))"
Try using SUMPRODUCT combined with unary operator:
Yellow cells would be the criteria (in your case, central!C3:C6).
Formula in cell C6 is:
=SUMPRODUCT(--(COUNTIF($A$6:$A$7,$C$1:$L$1)>0)*$C$2:$L$2)
Formula in cell C7 is:
=SUMPRODUCT(--($C$1:$L$1=$A$6:$A$7)*$C$2:$L$2)
First formula ignore duplicates in criteria and second one does not. Second one is easier to implement but if your criteria has duplicate values, it will duplicate result, so watch out and choose wisely. Just as example, notice if both of my criterias are the same, each formula returns a different output:
Anyways, when you are done choosing, you should be able to implement this in VBA code like this (or kind of):
Worksheets("units").Cells(2, 2).Formula = "=SUMPRODUCT(--(COUNTIF(central!$C$3:$C$6,pws!$C$1:$L$1)>0)*pws!$C$2:$L$2)"
SUMPRODUCT and Double Unary
Operators

How to stop excel from changing formula to referenced column range?

Not sure if my terminology is correct but the problem is that I have a vba module that I am using to analyze data(count unique values). The formula is fine and works when after I correct excels change.
VBA line to insert formula is
ws_MachineNumber.Range("D2").FormulaLocal = _
"=SUM(IF('Roh Daten_IA'!$B$2:$B$" & lastR_RDi & "= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$" & lastR_RDi & ",MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$" & lastR_RDi & ",'Roh Daten_IA'!$G$2:$G$" & lastR_RDi & ")),0))"
Which I am expecting to show up in the cell as
=SUM(IF('Roh Daten_IA'!$B$2:$B$296= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$296,MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$296,'Roh Daten_IA'!$G$2:$G$296)),0))
However for a reason unbeknownst to me excel keeps making it
=SUM(IF(#'Roh Daten_IA'!$B$2:$B$296= A2, 1/(COUNTIFS('Roh Daten_IA'!$B$2:$B$296,MachineNumber!A2,'Roh Daten_IA'!$G$2:$G$296,#'Roh Daten_IA'!$G$2:$G$296)),0))
not sure why it keeps referencing it that way. Prior to this I didn't have the abs ranges and figured that was the problem but the # symbol is somehow causing the formula to operate incorrectly and it gives a result of 0 for every occurrence regardless of what the correct evaluation should give.
Thank you

Excel - Inserting Cell Values into Ranges

I'm having problems inserting the value of a cell into a range. Right now I have:
=M3/SUM(M$3:M$13)
What I need is to be able to insert the value of another cell into that range, like:
=M3/SUM(M$A1:M$B1)
I have spent some time with the indirect function but I can't seem to get the syntax right. Does anyone know the correct syntax? Is indirect even the best/easiest/cleanest way to achieve my goal?
This will do what you want:
=M3/SUM(INDIRECT("M" & $A$1 & ":M" & $B$1))
Not sure if you want A1 and B1 absoluted so I did it anyway.

Dynamic formula with quotes inside a COUNTIFS function

I am trying to adapt the following formula (counting instances of values between 0 and 24):
=COUNTIFS(cancellations!AG2:AG408,">0",cancellations!AG2:AG408,"<24")
(formula evaluates to 75), to something like:
=COUNTIFS(cancellations!AG2:AG408,">0",cancellations!AG2:AG408,INDIRECT("" & "<" & B1*24 & ""))
but this evaluates to zero. (B1 = 1 in the above example.)
When I view the INDIRECT function inside the fx box it evaluates correctly. Not sure of what I am doing wrong.
When applied in a cell like so:
=INDIRECT("" & "<" & B1*24 & "")
It shows #REF!.
The INDIRECT function returns a reference to a range. You can use this function to create a reference that won't change if row or columns are inserted in the worksheet. Or, use it to create a reference from letters and numbers in other cells.
=COUNTIFS(cancellations!AG2:AG408,">0",cancellations!AG2:AG408,B1)
should work where B1 contains <24.
So, I read up the COUNTIFS() function in more detail and I found that the following works
=COUNTIFS(cancellations!AG2:AG408,">0",cancellations!AG2:AG408,"<"&B1*24)
Where:
B1=1

HLOOKUP in R1C1 notation

I'm using Excel VBA to insert an HLOOKUP function at the end of a given row. I've been attempting to use the R1C1 functionality in VBA for inserting the HLOOKUP function, which should appear as follows in Excel:
=HLOOKUP(D2,'DM NYASSOV'!3:34,32,0)
My issue is that I need the HLOOKUP to be dynamic enough that it can reference variables from the same row on which the HLOOKUP function is to be pasted.
Currently my VBA reads as follows:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],'DM RC[-19]!'RC[2]:RC[3],RC[3]-RC[2]+1,0)"
RC[-17] references the variable I'm looking for;
RC[-19] is the underlying book/tab identifier;
RC[2] contains the initial row value, and
RC[3] contains the final row reference range.
My main issue is with correctly identifying the dynamic range selection:
'DM NYASSOV'!3:34 / 'DM RC[-19]!'RC[2]:RC[3]
Any pointers are greatly appreciated.
I think you have a few issues here. First is using VBA to dynamically build a formula that then calculates a third value that then gets used in the formula which finally calculates a result. This seems like an unnecessary amount of runaround for a value that can be done with either a formula built on the front end or calculated via VBA in the back end. The second is all the dynamic lookup of it all. It's not so much a problem, but rather a lot to keep track of as you jump through the four hoops. Just the same...
The first parameters of your HLOOKUP can either be a value like "SM1804" or a reference to a cell that contains "SM1804". You can either use VBA to bring this value directly into the formula and save Excel some processing by having to lookup that value at formula processing time:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(" & Sheets(selectTab).Cells(r, 21).Offset(0, -17).value & ",'DM RC[-19]!'RC[2]:RC[3],RC[3]-RC[2]+1,0)"
Or you can stick the reference to the cell in there (which is what you are doing now):
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],'DM RC[-19]!'RC[2]:RC[3],RC[3]-RC[2]+1,0)"
On to the second parameter... I believe you have a sheet name in DM!RC[-19] This is totally OK, but you'll need to use 'Indirect' to get that sheet name into the HLOOKUP formula:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],Indirect("'" & DM!RC[-19] & "'!RC[2]:RC[3]"),RC[3]-RC[2]+1,0)"
...this is where things get a little tricky. If, for instance, in DM!RC[-19] you have the sheetname "Sheet1" then indirect is going to return: 'Sheet1'!RC[2]:RC[3] and your HLOOKUP will use that range to do the lookup... Doesn't make a lot of sense to do a HLOOKUP on a range with two cells. So I assume that you have number values in RC[2] and RC[3] there that represent rows. So really the indirect would have to look like:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],Indirect("'" & DM!RC[-19] & "'!R" & RC[2] & "C:R" & RC[3] & "C"),RC[3]-RC[2]+1,0)"
Now if your RC[2] has the value "4" and your RC[3] has the value "20" your indirect will return: 'Sheet1'!R4C:R20C and your HLOOKUP will use that value as the range in which it will look up.
You are also doing some math on those values in RC[2] and RC[3], so that's probably all good and doesn't need to be changed. You just need to keep in your head what VBA is going to return as a formula, and then what that formula is going to get from Indirect and then what the resulting HLOOKUP is going to find.
It's a lot to keep track of and may be simplified just by writing the formulas directly in the cell and copying down, or just doing the HLOOKUP functionality directly in VBA.

Resources