Add a cell formula in Excel via vba - vba

I’m not an Excel or VBA expert but I want to insert this current excel formula into cell’s using VBA.
Current Excel formula:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
VBA formula :
ActiveSheet.Range("a" & ActiveSheet.Rows.Count).End(xlUp).Offset(2, 12).Value = "=IF(OR(ISNUM(R[-3]C[-9])=FALSE;ISNUM(R[0]C[-9])=FALSE);'';IF(R[0]C[-10]='Total';R[0]C[-9]-R[-3]C[-9];''))"
It doesn’t work… Someone can help me please?

Try using .formula = instead of .value = in your VBA code.
Setting the .value of a cell simply copies in whatever value you specify. In this case, your formula is simply converted to a string value.
Using the .formula property, you are actually specifying the formula that gets used to compute the value, which is what you are looking for.

Can I first suggest a simplification of your formula, from:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
...to...
=IF(AND(C573="Total"; ISNUM(D570); ISNUM(D573)); D573-D570; "")
Then, I'd set a cell (the active cell in the example below) to use that formula using the VBA code:
ActiveCell.Formula = "=IF(...)"

Related

Remove certain string from word Excel VBA

So I have a certain word within a word in a cell in excel that I'm trying to remove. For instance in one of the cells I have P/N279-82345 and I'm trying to get rid of the P/N in the front but keep the rest. I know there is the like function but is there a way to use the like function to remove just the P/N?
Thank You
Answer is
x = Replace(ws2.Cells(i, 1), "P/N", "")
Use the replace function.
Syntax can be found here: https://support.office.com/en-us/article/REPLACE-REPLACEB-functions-8d799074-2425-4a8a-84bc-82472868878a
if you are looking for a VBA macro function you can use something like:
Cells(1, 1).Value = Replace(Cells(1, 1).Value, "P/N", "")
where the Cells value is the range you want to search.
If you are looking for an excel worksheet function, you can use substitute:
=substitute(A1,"P/N","")
where A1 is the cell that contains the value you want to substitute.

VBA formula inside cell

I need to put this formula inside a column subset range
=VLOOKUP(SUBSTITUTE(M3;"#";"");$AG$413:$AK$821;5;FALSE)
I wrote this code:
XML.Range("V3:V411").Formula = "=VLookup(Substitute(M3, ""#"", """"), $AG$413:$AK$821, 5, False)"".Value = .Value"
but doesn't work and I get "Select method of range class failed" error
I recommend using the R1C1 format, more stable in macros.
Also, just use the macro recorder.
Don't pass .Value to Excel, it has no idea what that means. What was the plan with that?
.FormulaR1C1 = "=VLOOKUP(SUBSTITUTE(R[2]C[12],""#"",""""),R413C33:R821C37,5,FALSE)"
Or with .Formula:
.Formula = "=VLOOKUP(SUBSTITUTE(M3,""#"",""""),$AG$413:$AK$821,5,FALSE)"

Writing formula into an Excel Range with Option Strict On

Is it possible to write formulas across a range in Excel from VB.Net? I'm using a String array to hold a list of formulas that I would like to apply to an Excel range, instead of looping through and writing them one at a time.
This line is what I am attempting to use to write to a range:
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).Formula = formulas
The formula is being written to Excel, but Excel is actually displaying the formula, instead of the calculated value. If I write each formula to each cell like this:
xlWorkSheet.Range("AF" & intCurrentRow.ToString).Formula = formulas(0)
xlWorkSheet.Range("AG" & intCurrentRow.ToString).Formula = formulas(1)
It works perfectly fine and Excel displays the calculated values as it should. Almost seems like I'm missing a step but I haven't been able to find anything in my research.
As soon as I hit post I figured out the problem. Instead of using the .Formula property of an Excel.Range, you have to use the .FormulaArray property instead.
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).FormulaArray = formulas

Using Excel Formula in VBA

Currently I have the following formula working perfectly when entered directly into the excel sheet.
=AVERAGEIFS(A:A,A:A,">="&10,A:A,"<="&11)
However, when I attempt to exectue it in VBA it says there is a formatting error.
Formula = Application.WorksheetFunction.AverageIfs(A:A,A:A,">="&10,A:A,"<="&11)
Is there any way to fix this?
You need to use Range() to define ranges in VBA:
Formula = Application.WorksheetFunction.AverageIfs(Range("A:A"), Range("A:A"), ">=10", Range("A:A"), ">=11")

Setting cell formula which includes "text constants" in Excel cell by VBA

In actual cell formula I can manually set the following formula.
=IF(MONTH(A15)<7,"FY "&YEAR(A15)-1&"/"&RIGHT(YEAR(A15),2),"FY "&YEAR(A15)&"/"&RIGHT(YEAR(A15)+1,2))
I am trying to set this formula by vba using the following code.
ActiveCell.formulaR1C1 = "=IF(MONTH(A15)<7,"FY "&YEAR(A15)-1&"/"&RIGHT(YEAR(A15),2),"FY "&YEAR(A15)&"/"&RIGHT(YEAR(A15)+1,2))"
The VBA compiler displays a compile error: Expected: end of statement. This appears to have a problem with the exclamation marks.
Does anyone now how to include a text constant in a cell formula set by vba code?
You need to use double quotes within the string like so:
ActiveCell.formulaR1C1 = "=IF(MONTH(A15)<7,""FY ""&YEAR(A15)-1&""/""&RIGHT(YEAR(A15),2),""FY ""&YEAR(A15)&""/""&RIGHT(YEAR(A15)+1,2))"
You only use FormulaR1C1 if you pass a reference in R1C1 format, but you're using A1 format. You can also shorten that formula if you wish:
ActiveCell.Formula = "=""FY ""&YEAR(A15)-(MONTH(A15)<7)&""/""&RIGHT(YEAR(A15)+(MONTH(A15)>=7),2)"