VBA Runtime Error '1004' when attempting to Insert Formula - vba

I've figured out how to insert a formula into a range of cells and managed to make it work once. Unfortunately, I can't get it to work with this formula. Instead I get an
Application-defined or object-defined error.
Here's what I'm attempting to run.
Sheets("P&L").Select
Range("A1:A250").Select
Selection.FormulaR1C1 = "=IF(ISNUMBER(LEFT(RC[+1],4)*1),LEFT(RC[+1]4,4)*1,)"
Selection.Columns.AutoFit
I suspect it has something to do with the * acting as a wildcard. I've put it in block quotes, but that just gives another error.
Any help is appreciated.

You've got an extra 4 in that formula.
Selection.FormulaR1C1 = "=IF(ISNUMBER(LEFT(RC[+1],4)*1),LEFT(RC[+1],4)*1, text(,))"

Related

Dynamic ActiveCell address in Excel VBA

Can someone help me debug this VBA macro?
I want the macro to set a value in a cell using a relative address. If the value in ActiveCell is "Primary", I want the macro to put the value "1.000" in the cell immediately to the right of the ActiveCell.
Sub SetSplitPercent()
If Worksheets("Reps To Users").ActiveCell.Value = "Primary" Then
col = ActiveCell.Column + 1
Cells(ActiveCell.Row, col) = "1.000"
End If
End Sub
The above macro throws the error "Application-defined or object-defined error" with the Cells() statement being the offending one.
Thanks in advance for the help.
I've tried several different approaches and haven't been able to get any of them to work without error. This approach seems like the closest I've gotten but still no dice.

How to do If Error If Vlookup with validations in vba macros [duplicate]

as a continuation from another question, I'm trying to solve my problems inserting a formula via VBA on a macro.
Here's my code:
Range("F1").Select
ActiveCell.Formula = "=IF(C1=""LPPD"";""MIPRU"";IF(C1=""LPGR"";""DCT"";IF(OR(C1=""LPFL"";C1=""LPCR"");""LADOX"";IF(OR(C1=""LPPI"";C1=""LPSJ"";C1=""LPHR"");""NOTMA"";""ERRO""))))"
For some reason, and the code doesn't show any errors, when I try to run it I get:
Run-time error ("Application-defined or object-defined error")
Worth mentioning I'm using Excel 2003.
Hope I'll be able to find my answer with you guys! Thanks in advance.
VBA is US-EN centric, so using the .Formula the formulas must be with , instead of ;:
Range("F1").Formula = "=IF(C1=""LPPD"",""MIPRU"",IF(C1=""LPGR"",""DCT"",IF(OR(C1=""LPFL"",C1=""LPCR""),""LADOX"",IF(OR(C1=""LPPI"",C1=""LPSJ"",C1=""LPHR""),""NOTMA"",""ERRO""))))"
Or you can use .FormulaLocal
Range("F1").FormulaLocal = "=IF(C1=""LPPD"";""MIPRU"";IF(C1=""LPGR"";""DCT"";IF(OR(C1=""LPFL"";C1=""LPCR"");""LADOX"";IF(OR(C1=""LPPI"";C1=""LPSJ"";C1=""LPHR"");""NOTMA"";""ERRO""))))"

Excel VBA FormulaR1C1 Error 1004

I am writing a macro that will insert a formula into a cell.
ActiveCell.FormulaR1C1 = "=IFERROR(((R[0]C[-2]*R[0]C[-3])+(R[0]C[-5]*R[0]C[-6]))/(R[0]C[-3]*IF(R[0]C[-6]=0,1,R[0]C[-6]),)"
I want it so that the last term IF(R[0]C[-6]=0,1,R[0]C[-6]),) to be equal to 1 if the condition is equal to 0.
When I run the the code, comes up with a Error 1004, Application-defined or object-defined error.
Can someone please help me in solving this problem?
Try using
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-2]*RC[-3])+RC[-5]*RC[-6])/RC[-3]*IF(RC[-6]=0,1,RC[-6]),"""")"
The problems I could see with your formula are that you had six opening brackets and only 5 closing and you didn't enter anything after the comma.
You can also write R[0] as just R meaning This Row.
You have bad parenthesizing. Try this:
=IFERROR(((RC[-2]*RC[-3])+(RC[-5]*RC[-6]))/(RC[-3]*IF(RC[-6]=0,1,RC[-6])),)
' ^^^
Also drop useless [0] and make life easier.

VBA Excel implement COUNTIF function

I am trying to implement a "COUNTIF()" function in my vba excel application. I know how to do this programatically but I want specifically to implement this as a formula so that later changes in the sheet will hold. This is the problematic line:
ActiveSheet.Cells(3, 20).FormulaR1C1 = "=COUNTIF(R11C7:R12C7;"">0"")"
It results in the following error:
Run-time error '1004': Application-defined or object-defined error
VBA defaults to US formatting unless otherwise specified - which you could do here using FormulaR1C1Local - so you need to use a comma separator, not a semicolon.

How do I convert this Excel formula into VBA code?

'I am using this formula in an Excel worksheet, in cell A6. It is working fine.
=IF(O6="Hand","Manual Entry",IF(O6="JET",R6,IF(O6="COKE","Red Bull",IF(O6="Freight","Logistics",IF(O6="TAX","Tax",IF(O6="TRANSFER COST","Transfer Cost Transactions",IFERROR(IF(FIND("INV#",R6,1)>=1,MID(R6,FIND("INV#",R6,1),10),""),"")))))))
Now, my question is: how do I convert this to VBA? I have tried recording it, and the code is as follows:
ActiveCell.FormulaR1C1 = _
"=IF(RC[14]=""Hand"",""Manual Entry JE"",IF(RC[14]=""JET"",RC[17],IF(RC[14]=""COKE"",""Red Bull"",IF(RC[14]=""FREIGHT"",""Logistics"",IF(RC[14]=""TAX"",""Tax"",IF(RC[14]=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",RC[17],1)>=1,MID(RC[17],FIND(""INV#"",RC[17]" & _
"""""),"""")))))))"
When I run this, I am receiving Run Time Error 1004: Application-defined or object-defined error.So I Changed this to something like this, this doing same as above formula except the find option, everything is running fine.
![VBA For Above formula][Any Help on the find?]
End sub.How do i get the fine option in the above VBA code.`
This works for me:
Range("L6").Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"
This is just your original Excel formula, but with the " characters escaped as "".
You need to escape your quotes.
ActiveCell.Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"
And use .Formula since you're using specific cell names that are not relative to the currently selected cell.