Excel SUMIFS formula with reference to other workbook - excel

Im trying to write a VBA code for which will fill my excel SUMIFS formulas.
In general SUMIFS will calculate sums based on other workbook data.
Other workbook filename is currently hardcoded in my macro, however i'd like to make it more flexible and instead of hard coding it i'd like to use a cell in which i put the file name.
so far macro creates the following formula and puts it in cell:
=SUMIFS([SW.xlsx]TeamAllocations!F$9:F$401,[SW.xlsx]TeamAllocations!$D$9:$D$401,$D16,[SW.xlsx]TeamAllocations!$B$9:$B$401,$B16)
what i'd like to have is kind of:
=SUMIFS([<variable evaluated based on a cell which has filename/filepath, eg B1>]TeamAllocations!F$9:F$401,[<variable evaluated based on a cell which has filename/filepath, eg B1]TeamAllocations!$D$9:$D$401,$D16,[<variable evaluated based on a cell which has filename/filepath, eg B1]TeamAllocations!$B$9:$B$401,$B16)
So in B1 cell i'd have "SW_01.xlsx" or "SW_02.xlsx", so the SUMIFS formula will always have up-to-date soruce.
Is that possible in excel?
Thanks
J.

If all your files have a fixed format, I think you can combine all of the files with power query.
You can do what you want more easily.
Combine Files:
https://chandoo.org/wp/combine-excel-files-using-power-query/
Group in PowerQuery (same as SUMIF):
https://support.office.com/en-us/article/group-rows-in-a-table-power-query-e1b9e916-6fcc-40bf-a6e8-ef928240adf1
Useful filter:
https://exceleratorbi.com.au/pass-excel-parameter-power-query/

Related

Excel Sumifs to where "sum_range" is the product of two ranges?

I'm trying to get this line of code to work:
=SUMIFS($A$30:$A$5141*$B$30:$B$5141,$C$30:$C$5141,1)
But Excel doesn't accept this as a valid formula.
However, if I create a dummy "SPILL range," I can get this to work.
I.e. if I put this code in cell "D1"
=$A$30:$A$5141*$B$30:$B$5141
Then this SUMIFS works fine:
=SUMIFS(D1#,$C$30:$C$5141,1)
Why don't SUMIFS work with dynamically created ranges? Other Excel functions seem to work okay.
Is there any way to compress the SUMIFS into one formula without using a help column or a helper SPILL function?
I'm using Excel 365.
Not all functions that take a range will also take an array - the VBA documentation makes this explicit in the case of SUMIFS() - the area to be summed must be a Range.
This SUMPRODUCT()-based formula may suit your purposes instead
=SUMPRODUCT(($A$30:$A$5141*$B$30:$B$5141)*(C30:C5141=1))

Evaluate an expression as a formula in Excel

I have created an expression using "&" to concatenate different cells, I created this:
=INDEX((Data_sheet!A1:M20440;Data_sheet!A20441:M40880;Data_sheet!A40881:M61320);20;5;D2)
Now, I have the expression that is a formula, but it is not evaluated as a formula when I paste it, I can see the whole expression in the cell, but I want to get is the value that is behind evaluating that formula. I want to know how to make it to evaluate as a formula (because I will have thousands of this expressions)
My guess is that you would have created the INDEX formula in excel using multiple other formulas.
Once you have created the formulas, copy all the formulas and paste it in a txt file. Then, copy all the formulas from txt file and paste it again in the excel where you want the formulas to perform. This is the simplest way of doing this without creating any macros or additional formulas
See this question for solutions:
How to turn a string formula into a "real" formula
In particular, also note the answer about Excel's hidden EVALUATE function, that can only be accessed after defining a name to use it.
https://www.vertex42.com/ExcelArticles/evaluate-function.html

How to translate this formula translated into VBA?

I have this formula below that I am attempting to use to perform different calculations depending on the data in the referenced cells. The formula works, but I need to have more flexibility on the data found.
=IF(AND(U1="APO 300%",$E2="P+"),P2-(SUMIF($F$2:$F$74,300%,H$2:H$74)*M2)-(SUMIF($F$2:$F$74,150%,H$2:H$74)*M2),IF(AND(U1="APO 300%",$E2="P"),H2,""))
Is there a code that can duplicate this?
Worksheets have an Evaluate method which can be used to do what you want:
shtObject.Evaluate("A1+B1") '<<< substitute in your formula

How do I convert COUNTIFS formula to SUM(IF to avoid #VALUE due to external links

I have an Excel formula which references several cells in other files, of the format:
=COUNTIFS('[FILE1.xlsx]Sheet 1'!$D$2:$D$5000,"A",'[FILE1.xlsx]Sheet 2'!$H$2:$H$5000,"B")
When I open this file, the cell value defaults to #VALUE (if FILE1.xlsx is closed).
Searching Microsoft Support (https://support.microsoft.com/en-us/kb/260415) this is because formulas such as SUMIF, SUMIFS, COUNTIF, COUNTIFS or COUNTBLANK automatically calculate on spreadsheet opening.
Microsoft's workaround is to replace COUNTIF with SUM(IF(Range=Condition,1,0) entered as an array. (There is no example for COUNTIFS.)
My question is: How do I change my formula to be of the format SUM(IF (AND?) ?
(I do not want my formula trying to recalculate if I open the spreadsheet with the source file closed.)
I've tried =SUM(IF(AND('[FILE1.xlsx]Sheet 1'!$D$2:$D$5000="A",'[FILE1.xlsx]Sheet 2'!$H$2:$H$5000="B"),1,0)) (as an array)
But this returns 0.
Any thoughts?
(Or must I revert to a clunky 3rd column in my source spreadsheet containing the COUNTIF logic so my file can just use SUM instead ...?)
The array equivalent to your COUNTIFS would be:
{=SUM(IF(('[FILE1.xlsx]Sheet 1'!$D$2:$D$5000="A")*('[FILE1.xlsx]Sheet 2'!$H$2:$H$5000="B"),1,0))}
But
=SUMPRODUCT(('[FILE1.xlsx]Sheet 1'!$D$2:$D$5000="A")*('[FILE1.xlsx]Sheet 2'!$H$2:$H$5000="B"))
should also work.

Formulas won't calculate automatically

I have a big sheet with a lot of formulas that have a dependency hierarchy between them. It starts with a cell with a date value. Then, cell x:y (and others), has formula depending on this date. Then cell w:z (and others) has a formula depending on cell x:y. And so on...
This main cell with a date value is filled using apache poi.
And now my problem: when I open the generated excel file, the date is there, but none of the formulas are calculated. They all have the error "A value used in the formula is of the wrong data type". It seems that when the formula try to evaluate it self the date isn't there yet.
Solutions:
1) If I click in the cell, and just press ENTER, the formula is correctly evaluated. But then I would have to do this for all cells.
2) I click in the date cell, copy it, and then paste it in the same place, and all formulas in the sheet are evaluated!
3) I could iterate in all cells in my application, evaluating each one with evaluateFormulaCell method from FormulaEvaluator class. But I have a lot of formulas and the performance of this is terrible.
Does someone have a solution for this?
Thanks!!
Your date value isnt recognized by the excel formulas as a date, what you could do is have another cell dat will first take the datevalue DATEVALUE() of the cell filled using Apache poi.
Then direct your first level formulas to that cell instead of the one filled using Apache poi
Because Excel don't calculale automatically, so just change this.
Excel 2010: File -> Options -> Formulas: Find Workbook calculation, and change to Automatically.
It worked for me.
To solve this, I changed all formulas in my template, to use instead of the regular formula sintax (=SUM(A:D)) the following sintax:
$[SUM(A:D)]
http://jxls.sourceforge.net/reference/formulas.html

Resources