How to translate this formula translated into VBA? - excel

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

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))

Excel SUMIFS formula with reference to other workbook

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/

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

A formula to copy the values from a formula to another column

I have a column of values created from a formula, I know I can copy the values over to another column by using the clipboard. BUT...I want my spreadsheet to be automatic whilst avoiding the use of VBA coding, so it would be ideal if I could create a formula for the next column which copies the VALUES over to the next column. Maybe an INDEX/MATCH kind of method but one that copies ONLY the values rather than the formulas.
So in essence I want a formula to copy the values from a formula....
You can use =A4, in case A4 is having long formula
Use =concatenate(). Concatenate is generally used to combine the words of several cells into one, but if you only input one cell it will return that value. There are other methods, but I find this is the best because it is the only method that works when a formula, whose value you wish to return, is in a merged cell.
For such you must rely on VBA. You can't do it just with Excel functions.
you can use those functions together with iferror as a work around.
try =IFERROR(VALUE(A4),(CONCATENATE(A4)))
What about trying with VLOOKUP? The syntax is:
=VLOOKUP(cell you want to copy, range you want to copy, 1, FALSE).
It should do the trick.
Copy the cell. Paste special as link. Will update with original.
No formula though.

How to simply excel formula?

I have a query that will generate the following formula in an array from SQL and set into a range in worksheet, but it's difficult to read:
=SUMIF(INDIRECT(ADDRESS(ROW()-1,5)&":"&ADDRESS(ROW()-6,5)),"+",INDIRECT(ADDRESS(ROW()-1,COLUMN())&":"&ADDRESS(ROW()-6,COLUMN())))-SUMIF(INDIRECT(ADDRESS(ROW()-1,5)&":"&ADDRESS(ROW()-6,5)),"-",INDIRECT(ADDRESS(ROW()-1,COLUMN())&":"&ADDRESS(ROW()-6,COLUMN())))
So dose excel contain any method to change only the address part INDIRECT(ADDRESS(ROW()-1,5)&":"&ADDRESS(ROW()-6,5)) to E10:E5 for easy to trace?
Because of the formula is needed to be dynamic generate before set to worksheet (i.e. dynamic column and row), so it should be needed to simplify after set the array to cell.
Any method can do the similar thing like Evaluate function in excel but only apply for INDIRECT and ADDRESS which allow user to read the simply formula like =SUMIF(E10:E5,"+",J10:J5)-SUMIF(E10:E5,"-",J10:J5)
To simplify the formulas, you could enter in RC notation by changing the reference style to R1C1 or using .FormulaR1C1. The advantage of RC notation is that the formula text is consistent down the whole column. For your formula above you could enter either as:
=SUMIF(R[-6]C5:R[-1]C5,"+",R[-6]C:R[-1]C)-SUMIF(R[-6]C5:R[-1]C5,"-",R[-6]C:R[-1]C)
or =SUM(SUMIF(R[-6]C5:R[-1]C5,{"+","-"},R[-6]C:R[-1]C)*{1,-1})
and the formulas should be easy to read in A1 notation.

Resources