How to write sumproduct formulas in VBA? - excel

I have a workbook with a lot of sheets and there is one sheet that I would like to be calculated only when pushing a button. This sheet is very small and contains only 10 formulas: please see the picture below.
Many of these formulas are sumproduct :
Formula 1 =SUMPRODUCT('Loan Overview'!$S$2:$S$55,'Loan Overview'!$AE$2:$AE$55*('Loan Overview'!$L$2:$L$55=TRUE))/SUMIF('Loan Overview'!$L:$L,TRUE,'Loan Overview'!$S:$S)
Formula 2 =SUMPRODUCT('Loan Overview'!$S$1:$S$500,'Loan Overview'!$T$1:$T$500)
Formula 3 =SUMIFS('Loan Overview'!$AJ$1:$AJ$500,'Loan Overview'!$L$1:$L$500,TRUE)/(SUMPRODUCT(--(LEN('Loan Overview'!$B$1:$B$500)>0))-(1+COUNTIF('Loan Overview'!$AJ$1:$AJ$500,"N/A")))
I wanted to know if there is an easy way to convert excel formulas into VBAs and then assign them to a button.

You can use several Excel function as a VBA function.
This is explained in this URL about Application.Worksheetfunction.
So, the functions you're looking for, can be used as Application.WorksheetFunction.SumProduct(), Application.WorksheetFunction.SumIfs(), ...

Related

Sum up values from different worksheets

I'm building the budget template in Excel. In the Summary sheet, I want to sum columns from different worksheets by the corresponding cell.
enter image description here
In my way, I get all sheet names (by VBA code, it will automatically update when creating new worksheets), then use TEXTJOIN to have all reference cells I want in a text. It looks like this: Sheet1!$B$2,Sheet2!$B$2,Sheet3!$B$2,Sheet4!$B$2,Sheet5!$B$2,Sheet6!$B$2,Sheet7!$B$2
I guess we can have some way to put that text in SUM function in Excel to sum up all reference cells value, but I haven't found how to do that yet.
If anyone can have a look and give me a solution, it's much appreciated.
Thank you as always
As I understood the question you can do like this in reference cell:
=SUM(Sheet1:Sheet7!C2)
=SUM(Sheet1:Sheet7!C3)
=SUM(Sheet1:Sheet7!C3)
=SUM(Sheet1:Sheet7!C5)
=SUM(Sheet1:Sheet7!C6)
--------

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/

VBA to do Vlookup in Excel sheet with Vlookup formula

I m new to Excel VBA. i need to do a vlookup in excel sheet with this vlookup formula:
=INT(IFERROR(VLOOKUP(A:A,[sheet.xls]Sheet1!B:F,5,FALSE),0))
Could any one guide me to apply this vlookup using VBA
Basic approach:
myRange.Formula = "=INT(IFERROR(VLOOKUP(A:A,[sheet.xls]Sheet1!B:F,5,FALSE),0))"
It could be worth that you give also a look at .FormulaR1C1 property which might be more explicit from VBA. If you don't use FormulaR1C1 syntax, pay attention to Absolute/relative address.
mySheet.Range("X2").Formula = "=INT(IFERROR(VLOOKUP($A:$A,[sheet.xls]Sheet1!$B:$F,5,FALSE),0))"
If you don't know how to do something with VBA, use the Macro Recorder. Its output is crappy and always needs to be cleaned up, but at leat you get an answer in 5 secs and it's an easy learning tool.
Eventually, enter the formula manually in a cell - say in X2 - then from the Immediate window, you type Print range("x2").Formula (or formulaR1C1) to see the exact formula to put in your code.

Look up multiple values in a list and sort them in a new sheet

I have a data sheet that contains 9 columns corresponding to a name. But the names are repeated. So I want to create new sheets for all names and filter their data from the data sheet. The sheets must be updated when any changes are made on the data sheet.
I tried VLOOKUP but had some various problems, and it didn't work well for me. I've found a way that may help, but it's prepared for Excel. ( https://fiveminutelessons.com/learn-microsoft-excel/use-index-lookup-multiple-values-list )
In OpenOffice ROW(1:1) doesn't give a result. Dragging as an array also doesn't change the cells in the formula. So are there any alternative ways to solve this problem? (OpenOffice solutions are preferred).
The example from the link in the question almost works in LibreOffice. To fix it, instead of ROW(1:1), use ROW($A$1:$A$6).
I am not sure what ROW(1:1) does in Excel, and I could not find an explanation online. In Calc, ROW($A$1:$A$6) returns an array of 1 column x 6 rows: {1;2;3;4;5;6}.
So here is the full formula using the example.
=IF(ISERROR(INDEX($A$2:$C$7,SMALL(IF($A$2:$A$7=$A$9,ROW($A$2:$A$7)),ROW($A$1:$A$6))-1,3)),"",INDEX($A$2:$C$7,SMALL(IF($A$2:$A$7=$A$9,ROW($A$2:$A$7)),ROW($A$1:$A$6))-1,3))
Be sure to enter it as an array formula with Ctrl+Shift+Enter

VLookUp Between Two Workbooks (No VBA)

I have two workbooks - let's call them workbook1 & 2. In workbook1 I want Column A to be autofilled with information stored in workbook2. However, I have come into two problems.
First, the formula returns a #N/A error in cell A2 of workbook1, despite being the same formula used in cells A3 & A4.
Second, the formula returns the wrong value for A4 despite the formula being written as approximate match (True), and the text in column B being identical.
The formula in question is:
(A2) =VLOOKUP([#[Vendor Name]],'Vendor Name-Code Cross Ref.xlsx'!Table1[#All],1)
(A4) =VLOOKUP([#[Vendor Name]],'Vendor Name-Code Cross Ref.xlsx'!Table1[#All],1)
The workbooks are below in numerical order.
I apologize for the simplicity of this question. I have tried a couple of things, I found online including this answer here and here. And I can't get it to work. I am happy to try another formula if there is a better tool for the job. I have been away from Excel for a few months and can't recall how to set this up...sorry for the easy question. Also, as I am not all too familiar with VBA I have not tried a VBA solution and that is why I listed that in the title as such.
Thanks for your help.
Couple options:
INDEX/MATCH: =INDEX('Vendor Name-Code Cross Ref.xlsx'!Table1[Vendor Code],MATCH([#[Vendor Name]],'Vendor Name-Code Cross Ref.xlsx'!Table1[Vendor Name],0))
Switch the column order in Workbook 2 and continue using VLOOKUP, but with exact match.
If you have access to the new Excel functions, use XLOOKUP.

Resources