Excel VBA formula as text issue - vba

In my Excel 2016 VBA I have this code:
Range("M2").Select
ActiveCell.FormulaR1C1 = "=IFERROR(IF(E2=""Sgl"",K2,L2),"")"
when it gets placed in the cell it is the formula not the result. To manually fix this I have to change from Text to General and also the actual code needs changing:
Cell M2 says: =IFERROR(IF(E2="Sgl",K2,L2),")
It should say: =IFERROR(IF(E2="Sgl",K2,L2),"")
with the extra " at the end before the closing parenthesis
This was recorded (I removed the RC to the absolute cell reference initially when it wouldn't work), so I'm not sure what caused this or how to resolve it.
Any help would be appreciated.
Thanks

I think you just need to add the extra quote at the end. Because the quote denotes a string, you need to make two of them to yield one -- which means you need four to yield two.
Range("M2").Formula = "=IFERROR(IF(E2=""Sgl"",K2,L2),"""")"
Other notes of interest:
You don't need to Range().Select and then work against ActiveCell. Just invoke the methods/properties directly on the Range Object
I think in your case Formula will work. R1C1 is handy when the formulas are relative, but in this case, you are referencing actual cells. There is nothing wrong with what you did, but FYI

To generate the two double quotes ("") in IFERROR, you need to put 4 double quotes ("""") in the vba string literal.

I fixed this by removing the IFERROR part as if there was no value in the proceeding columns it simply left it blank anyway rather than showing an error message:
Columns("M:P").Select
Selection.NumberFormat = "General"
Range("M2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-8]=""SGL"",RC[-2],RC[-1])"
Range("M2").Select

Related

Is there a way to use VBA for Excel to output a formula to a specific cell in an Excel worksheet?

I am fairly new at programming. I feel like this should be a simple fix but I cannot find anything that works. I am trying to use a Select Case structure to write a different formula to a cell depending on the selected case. If I type:
Sheet1.Range("g10").Value = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
I get an error:
Run-time error '1004'
Application-defined or object-defined error.
I can get it to work if I include spaces like so:
Sheet1.Range("g10").Value = " =IF(SUM(F10)>0,SUM((F10-15)),"") "
but then Excel puts it into the cell like text, not as a formula (it doesn't do anything).
I have tried a few different methods but have run into the same issue. Is it possible to do this?
I apologize if this has been asked and answered, but I wasn't able to find anyone referencing this specific problem.
You don't need to sum a single cell ie SUM(F10) Also SUM((F10-15)) is the same as F10-15.
Try this: Sheet1.Range("g10").Formula = "=IF(F10>0,F10-15,"""")" Also note, you needed to double the double quotes towards the end.
Note, you can do this on a range too like this:
Sheet1.Range("g10:g20").Formula = "=IF(F10>0,F10-15,"""")" and it is smart enough to increment the references for you for the 10 cells.
Wrong property. You need .formula not .value
worksheets("sheet1").range("g10").formula = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
You should be able to type out the formula and then turn on the Macro Recorder and double-click on the cell with the forums, then turn off the Macro recorder and examine your code. That should give you what you want.

Error 1004 when inserting formula into cell using macro

I'm trying to use a macro to insert an equation into a cell. The equation works fine if I copy it in myself but I need to copy it to 6000 cells in each or four worksheets. This question seems pretty common, but the usual answer of replace ";" with "," doesn't apply. The first line catches error 1004.
Range("J1").FormulaLocal = "=IF(ISERROR(F1),"",IF(ISTEXT(F1),"",F1))"
Range("J1:J6000").FillDown
I also tried using .formulaLocal but that doesn't seem to help.
You need to use double quotes to leave one quote:
Range("J1").FormulaLocal = "=IF(ISERROR(F1),"""",IF(ISTEXT(F1),"""",F1))"

IF function in VBA

I want the create the following:
If a cell in column C contains the value TRUE, the corresponding value in column B should be copied to the corresponding cell in column AJ.
This is what I have so far:
Range("AJ2").Select
ActiveCell.FormulaR1C1 = "=IF(C[-33]=TRUE,B:B,0)"
Range("AJ2").Select
Selection.AutoFill Destination:=Range("AJ2:AJ1324")
The problem occurs when I run the macro. The following statement is copied in the cells in column AJ:
=IF(C:C=TRUE;B:(B);0),
where I expect the following:
=IF(C:C=TRUE;B:B;0)
Could someone, please, tell me where my mistake is?
Thanks
ActiveCell.FormulaR1C1 = "=IF(C[-33]=TRUE,RC2,0)"
You are mixing R1C1 and A1 type references in the formula. B:B is not an R1C1 reference.
If you want:
=IF(C:C=TRUE;B:B;0)
Then you need to use:
"=IF(C[-33]=TRUE,C[-34],0)"
I suspect that may not be the formula you really want, but you have not addressed that issue.
One method of obtaining the correct formula is to enter what you really want into the cell, then examine the cell.FormulaR1C1 property to see how that formula is formed in the different notation.
I suspect, from your description, that what you really want would be created by:
"=IF(RC[-33]=TRUE,RC[-34],0)"
which would show, in the cell, as:
=IF(C2=TRUE;B2;0)
or
"=IF(RC3=TRUE,RC2,0)"
producing
=IF($C2=TRUE;$B2;0)

Run time error 13 type mismatch conditional formatting

I have code:
Dim Formul1 As String
Dim Formul2 As String
Formul2 = "=AND(R[1]C<=R[1]C[-1];(R[1]C+7)>R[1]C[-1])"
**Formul1 = Application.ConvertFormula(Formula:=Formul2, fromreferencestyle:=xlR1C1, toreferencestyle:=xlA1)**
With Range("$H$6:$FH$50")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:=Formul1
.FormatConditions(1).Interior.ColorIndex = 3
End With
and it gives me an error in highlighted place Run time error 13 type mismatch conditional formatting, I dont know why, any ideas?
Issue in Formul2, Change the AND Condition.. Use Comma instead of Semicolon.. should go fine.
Formul2 = "=AND(R[1]C<=R[1]C[-1],(R[1]C+7)>R[1]C[-1])"
In addition to your Formul2 using the semi-colon as the system list delimiter when your actual system is using the comma, you are also going to have to implement the RelativeTo parameter of the ConvertFormula method. An xlR1C1 style formula with relative cell references looks exactly the same no matter where it is on a worksheet. However, an xlA1 style changes depending on where it is.
The nature of what you are attempting to accomplish is not entirely clear but I would make an intelligent guess that you need to use RelativeTo:=Range("H6") as H6 is the cell in the top left corner of your CF rule's Applies to:.
Formul1 = Application.ConvertFormula(Formula:=Formul2, fromreferencestyle:=xlR1C1, toreferencestyle:=xlA1, RelativeTo:=Range("H6"))
It also seems likely that you may want to lock the column references for that wide Applies to: so that only columns G and H are considered but you will have to verify your intentions in that regard.
FWIW, you do not have to change an xlR1C1 style formula to xlA1 style before using it to create or modify a FormatConditions Collection Object. Either style is accepted.

Formula R1C1 issues using IF

this is new for me. I have an issue trying to run a formula in VBA using IF.
The idea is to include in AR3 column a modification date when in C3 there contains information.
This is what I wrote:
Range("AR3").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]="";"";IF(RC[-44]="";NOW();RC[-44])"
Thanks in advance!!
A couple of things are not right with your formula:
You need to escape the double quotes (with double quotes... so four in total)
You are missing a closing parenthesis
This is optional, but you don't need to select the range
You can use the following corrected statement:
Range("AR3").FormulaR1C1 = "=IF(RC[-3]="""","""",IF(RC[-44]="""",NOW(),RC[-44]))"
Next time you run into this kind of trouble, just use the macro recorder: click on "Record macro", and enter your formula. Plus, you can always change the display in Excel to the R1C1 Reference style (Options > Formulas > R1C1 reference style) when that is helpful (Personally I've mapped this change to a shortcut key combination so I can easily get column numbers if needed)