How to simply excel formula? - excel

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.

Related

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/

Excel Vlookup with cell reference

I have a cell range that I named "cell_range" in Excel. I want to extract the fourth row and fifth column from this table. The formula,
=vlookup(4,cell_range,5)
gives me the value I am looking for.
However, I also have the text "cell_range" in cell A1. Instead of typing out "cell_range" in my formula, I want to reference "cell_range" indirectly by referencing cell A1. The formula
vlookup(4,A1,5)
is giving me a "#N/A" error.
How can I indirectly reference the table "cell_range" in my formula?
Use INDIRECT:
=VLOOKUP(4,INDIRECT(A1),5)
While you can use INDIRECT to perform this kind of stuff, I steer clear of it for reasons I've outlined at https://chandoo.org/wp/2014/03/03/handle-volatile-functions-like-they-are-dynamite/
In this particular case I'd use a lookup table and either CHOOSE or INDEX, as demonstrated in the screenshot below:
Note that you need to pre-specify your areas with my approach.
Here's the syntax for CHOOSE
=CHOOSE(index_num,value1,value2,...)
Here’s the translation from Microsoft-speak into Jeff-speak:
=CHOOSE(Which area do you want?, First area, Second area, ...)
If using a dropdown or a cell reference to provide the index_num argument, you simply use a lookup table that converts the output of the dropdown or cell input into an index number that tells CHOOSE which range from the list should be used.
In this example, you’re using this approach to choose which area on the spreadsheet to sum. But CHOOSE and INDEX can be used to do a lot more than that. For instance, you could use them to allow a user to dynamically pick which lookup table to use when doing a VLOOKUP. Or you could let the user dynamically pick which range in which sheet to user for some calculations do. Very powerful stuff indeed!

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

Insert a formula in a range of cells via VBA using reference cells

I have a code that loops through and calculates a formula for a column of inputs for a row of dates. It is rather slow looping and writing in VBA so I was hoping to instead paste the formula in the cells instead of the computed value. My plan is to create a range for the results and set the range equal to the formula.
However I am stuck with the formula. The formula uses the previous answer plus other data and I am stuck trying to create the formula for the range so the reference cells move for each cell in the range.
The formula will look like this for cell N7
=IF(M7<>0,(M7-$D7)^(1/30.4),$B7*(1-$C7)^(1/$E7))
The formula will look like this when copied to cell O7
=IF(N7<>0,(N7-$D7)^(1/30.4),$B7*(1-$C7)^(1/$E7))
For O8 it would look like this
=IF(N8<>0,(N8-$D8)^(1/30.4),$B8*(1-$C8)^(1/$E8))
I understand how to loop through each cell and write the formula, but I don't think it would be much faster than calculating it and then writing the answer. I was hoping there is a way to set the range equal to a formula that would populate the correct cell references.
Would ActiveCell.Offset be a solution?
Use relative references:
.FormulaR1C1 = "=IF(RC[-1]<>0,(RC[-1]-RC4)^(1/30.4),RC2*(1-RC3)^(1/RC5))"
You can use this on a range to populate in one go if you need rather than looping through with something like this:
Range("N7:P20").FormulaR1C1 = "=IF(RC[-1]<>0,(RC[-1]-RC4)^(1/30.4),RC2*(1-RC3)^(1/RC5))"
R is row and C is column relative to self negatives have to be in []
You can also do this by using A1 Notation.
Range("N7:O8").Formula = "=IF(M7<>0,(M7-$D7)^(1/30.4),$B7*(1-$C7)^(1/$E7))"
There are advantages using R1C1 Notation but for this one, you better off using this, easier to read. HTH.

Using VSTO with Excel, is it possible to apply an Excel formula to a Range object directly?

Say I have a Range object for cells A1:A10 in my worksheet. These cells all contain strings of varying length, and I would like to know the length of the longest string.
Within Excel, I could use an array formula like {=MAX(LEN(A1:A10))} to find this value. I think I could set some other cell in the worksheet to use this formula and retrieve the value, but I'd rather not alter the sheet.
Is there some way I can apply this formula (or any formula, really) to a Range object directly in code and retrieve the result?
Alternatively, is there some other easy way to find this value without altering the content of the worksheet and without looping through each cell in the range?
You can use the Application.Evaluate function, e.g., in VB.Net:
MessageBox.Show(Application.Evaluate("=MAX(LEN(A1:A10))"))

Resources