Can´t add = in formula with VBA, Excel - vba

When I try to make a dynamic formula with VBA in excel i get this error message.
This line is just fine:
ActiveCell.Value = "IF(SUM(" & Range("A1:A5").Address & ")*0,1>" & Range("B1").Address & ";" & Range("C1").Address & ";SUM(" & Range("A1:A5").Address & ")*0,1)*-1"
If i add = in front of my IF-statement like this, I get the error.
ActiveCell.Value = "=IF(SUM(" & Range("A1:A5").Address & ")*0,1>" & Range("B1").Address & ";" & Range("C1").Address & ";SUM(" & Range("A1:A5").Address & ")*0,1)*-1"
If i just add the = sign after testing the first code. It runs fine in excel.
What am i doing wrong here?
Don't mind the ranges and stuff. They are only placeholders to make the example as similar to my code as possible.

To enter a formula in a cell you have to use the .Formula property instead of .Value.
Try this
ActiveCell.Formula = "=IF(SUM(" & Range("A1:A5").Address & ")*0,1>" & Range("B1").Address & ";" & Range("C1").Address & ";SUM(" & Range("A1:A5").Address & ")*0,1)*-1"

Related

Type mismatch when setting a formula into a cell with VBA

Worksheets("sheet2").Range("C2").Formula = "=AVERAGEIFS(Sheet1!E:E,Sheet1!A:A," >= "&A2,Sheet1!A:A," < "&B2)"
I have run the above code in Excel 2007 and I'm getting a
run time error 13 type mismatch
The above code is used to do average operation from sheet 1 and enter in sheet 2. I request some help in correcting the error.
The issue is you need to escape the quotes with another quote. That means all quotes " in the formula become "".
If the formula in that cell should be
=AVERAGEIFS(Sheet1!E:E,Sheet1!A:A,">=" & A2,Sheet1!A:A,"<" & B2)
and you put it into a string between double quotes " your formula here ", then you need to escape all quotes within the formula and change a " into a "" like below:
Worksheets("sheet2").Range("C2").Formula = _
"=AVERAGEIFS(Sheet1!E:E,Sheet1!A:A,"">="" & A2,Sheet1!A:A,""<"" & B2)"
Another option would be using the Chr(34) instead of a ".
Worksheets("sheet2").Range("C2").Formula = _
"=AVERAGEIFS(Sheet1!E:E,Sheet1!A:A," & Chr(34) & ">=" & Chr(34) & " & A2,Sheet1!A:A," & Chr(34) & "<" & Chr(34) & " & B2)"
This is technically the same but not very human readable in this case.

Inserting formula using VBA in Excel does not work

First of all, I have tried to look at all the other examples of adding a formula using VBA, and I think I have tried to apply all the answers in this code
Sub AddFormulas(SheetName As String)
Dim i As Integer
'Switch worksheet
Set Wksht = ThisWorkbook.Worksheets(SheetName)
Wksht.Activate
Application.Calculation = xlCalculationManual
i = 2
While Not IsEmpty(Cells(i, 1))
Wksht.Cells(i, 18).Formula = "=IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")"
i = i + 1
Wend
Application.Calculation = xlCalculationAutomatic
End Sub
But still, it gives me this anoying error, that I can't interpret
If I change my line to
Wksht.Cells(i, 18) = "'IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")"
Then I get no error, and the correct formula is added, although as a text string
What is wrong, since it would not add what to me looks like a valid formula?
//V
The Formula property requires formulas to be written in English, i.e. English function names (not an issue here) and commas as separators rather than semi-colons.
So your statement should be:
Wksht.Cells(i, 18).Formula = "=IFERROR(VLOOKUP(A" & i & "," & Wksht.Cells(1, 18) & "!$A:$A,1,FALSE)," & Chr(34) & "MISSING" & Chr(34) & ")"
If you don't mind having "portability" issues, you could also use the FormulaLocal property, i.e.
Wksht.Cells(i, 18).FormulaLocal = "=IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")"
Write the formula as it should be in Excel, select it and run the following code:
Public Sub PrintMeUsefulFormula()
Dim strFormula As String
Dim strParenth As String
strParenth = """"
strFormula = Selection.Formula
strFormula = Replace(strFormula, """", """""")
strFormula = strParenth & strFormula & strParenth
Debug.Print strFormula
End Sub
It should print it as it should be.
I believe your code is giving error becouse of this Wksht.Cells(1, 18) part of line in this row Wksht.Cells(i, 18).Formula = "=IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")". Make sure that Wksht.Cells(1, 18) really contains name of worksheet your are trying to address. If this cell is blank, you will receive previously mentioned error.

ranking data using vba

i come to ask your help i need to rank data using vba i have a block of results in column D and i want to rank them in column E without skiping any value so i tried this vba code but it gives me only zeros in all the column then my computer becom slow untill i close the excel file this is the vba code i am using if anyone can help me :
Sub Mactro5()
LastRow = Range("D" & Cells.Rows.Count).End(xlUp).Row
Range("E2:E" & LastRow).Formula = _
"=IF(D2=" & Chr(34) & Chr(34) & "," & Chr(34) & Chr(34) & ",SUMPRODUCT((D$2:D$" & LastRow & ">D2)/COUNTIF(D$2:D$" & LastRow & ",D$2:D$" & LastRow & "&" & Chr(34) & Chr(34) & "))+1)"
End Sub

How to insert formula into cell

Please assist me with the following code. I'm not sure why I keep getting the error: expected end of statement
Range("E" & i).Formula = "=""19""&Left(D" & i &"; 2)&""/"" & Mid(D" & i&"; 3; 2)&""/"" &Right(D" & i&";2)"
You have both a quote and an ampersand problem. Here is what is probably the correct line (can't be sure since you didn't tell us the desired end result). Note that I put spaces around all of the unquoted ampersands.
Range("E" & i).Formula = "=""19""&Left(D" & i & "; 2)&" & """/"" & Mid(D" & i & "; 3; 2)&" & """/"" &Right(D" & i & ";2)"

problems with VBA code application or object error

I have coded some code in VBA my code breaks at this line raising Application defined or object defined error.
.Formula = "=IF(AND(chr(34) & ' & chr(34) & Criterion " & i & "'!" & cellAdress & ">=1;chr(34) & ' & chr(34) & Criterion " & i & "'!" & cellAdress & "<=4);chr(34) & ' & chr(34) & Criterion " & i & "'!" & cellAdress & ";0)"
I really tried to check what is wrong but it looks fine too me. Please let me know what could be wrong and how to fix it.
Thank you
I think you want
.Formula = "=IF(AND('Criterion " & i & "'!" & cellAdress & ">=1;'Criterion " & i & "'!" & cellAdress & "<=4);'Criterion " & i & "'!" & cellAdress & ";0)"
At least this produces a valid and sensible cell formula, your's does not.
With cellAdress set to "A1" and i set to 10, the result would be:
"=IF(AND('Criterion 10'!A1>=1;'Criterion 10'!A1<=4);'Criterion 10'!A1;0)"
Ok I understand it now. My VBA also doesn't raise any errors until I run. Maybe I'm looking on this code for too long and go crazy. My cell Address contains D18 and i contains 1. I do have a sheet called Criterion 1 and the cell in this sheet has a value of 2 (i also tried when it was empty). Still this error is raised and I don't know what is causing it.