How to convert this formula into a VBA code? - excel

I'm starting to study VBA but even searching for the functions needed for this I could not mount the VBA code. I searched for the functions to simulate INDEX and MATCH in VBA, but I could not mount the code correctly.
I have a table to fill where in each cell is the following formula, for example in cell C11:
=IF(INDEX('Sheet1'!$G:$G;MATCH($B11&C$10;'Sheet1'!$B:$B&'Sheet1'!$A:$A;0))<>"";"sm";
IF(INDEX('Sheet1'!$E:$E;MATCH($B11&C$10;'Sheet1'!$B:$B&'Sheet1'!$A:$A;0))<>"";"ok";0))
I would like to transform this formula into a VBA code.
Thanks in advance for the help!

Related

vba macro excel - formula question after running a macro

i have a simple question but i can't find an answer in google.
i have the following simple code:
cells(1,1)= cells(1,2) + cells(1,3)
i want to be able to show the user the result in cells(1,1) but that he can also see how i got it (if he stands on the cell he should see that i did =B1+C1).
how can i do it?
moreover, i want him to be able to change the numbers in B1,C1 and get a new result in A1 like regular he could do in regular excel function
how can i do it?
thanks!
I think you may want to just add a formula to the cell and not put in a value.
Check this out.
VBA To Add Formula To Cell
Not exactly what you are asking but it shows you how to add a formula in your vba code.

INDIRECT FORMULA

I am having trouble working out an error I am having with the formula below, I am a bit of a novice and its been more trial and error than anything so far but, I was hoping someone with a far greater knowledge could point out where I am going wrong.
The formula works fine without the indirect element I.e.
=SUMPRODUCT((PARROT!$o$12:$o$250<>"COMPLETE")*(PARROT!$n$12:$n$250<TODAY()))
but I want to replace the sheet name with an INDIRECT address as the name of the sheet could change
so the formula I came up with is but, it has an error:
=SUMPRODUCT(INDIRECT("'"&$A3&"'!$o$12:$o$250"),"<>"&"Complete")*(INDIRECT("'"&$A3&"'!$N$12:$N$250")<TODAY())
Help would be very much appreciated.
From your formula it seems you are just counting rows where O12:O250 is not equal Complete and N12:N250 is earlier than today. So, you can use COUNTIFS() formula with INDIRECT() easily. Try below.
=COUNTIFS(INDIRECT($A$3&"!$O$12:$O$250"),"<>Complete",INDIRECT($A$3&"!$N$12:$N$250"),"<"&TODAY())
If you need to sum values from a column then SUMIFS() will be your friend.

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.

Working SUMPRODUCT function on Google Sheets does not work in Excel

I have a function here that is meant to calculate how many leads I have in each country.
=sumproduct($A2=(vlookup(id_leads!$A:$A,country_leads!$A:$B,2,0)))
On sheet id_leads,$A:$A contains the lead ids
On sheet country_leads Column A contains the lead ids. Column B contains the country of which the lead is located.
Can someone explain why it fails in Excel (I get a #VALUE! error), but works fine in Google Sheets? A suggestion to make this formula work in Excel would be appreciated. I've tried to use COUNTIF and SUMIF, but couldn't figure it out.
Thanks in advance!
You need to use COUNTIFS() wrap by SUM() in an array formula, something like:
=SUM(COUNTIFS(D:D,B1:B3,E:E,A1))
Being an array it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
Note the 2nd and 4th criteria need to be limited to the data set and not a full column reference.
So your formula would be something like this:
=SUM(COUNTIFS(country_leads!$A:$A,id_leads!$A1:$A100,country_leads!$B:$B,$A1))

Excel to Google spreadsheet - formula conversion

I have the following formula that works out filtered by multiple conditions.
{=SUM(COUNTIFS('List'!$H:$H,TRANSPOSE('Assignee'!$C2:$C6), 'List'!$U:$U, 'Proceed'!$B2:$B8)))}
So I converted to google sheeets formula using ARRAYFORMULA Function for array-evaluated but it doesn't worked.
=ARRAYFORMULA(SUM(COUNTIFS('List'!$H:$H,TRANSPOSE('Assignee'!$C2:$C6), 'List'!$U:$U, 'Proceed'!$B2:$B8))))
I made a sample document. Open sheets
You can check a formula in the summary sheet.
How can I solve this?
It should work. Maybe you forgot to array-evaluate it?
Double click the cell and press Ctrl+Shift+Enter OR, wrap the whole formula with =ArrayFormula()
=ARRAYFORMULA(SUM(COUNTIFS('List'!$H:$H,TRANSPOSE('Assignee'!$C2:$C6), 'List'!$U:$U, 'Proceed'!$B2:$B8))))

Resources