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.
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""))))"
So I'm a bit new to excel VBA, and I'm creating a macro to run on financial worksheets. I want to shift the values in the totals to the right place, as they are a column to the left of the actual data (these weren't created by a formula, they were generated by a different program and are fixed text). The shifting I managed to do just fine. The problem here is finding where the totals column is, as it varies between worksheets.
This is what I have so far.
For totalRow = 7 To 2000
With ws
If ws.Visible = True Then
If InStr(Range(totalRow, "A").Value, "Totals:") > 0 Then
Exit For
End If
End If
End With
Next totalRow
Yet for some reason, it's giving me an error when I try to run it. I know it's probably something simple I'm overlooking, because I cannot for the life of me figure out the problem. I've tried using a Do-Until loop, same issue. Is it a problem with the variables I'm using?
Several suggestions:
This is the basic problem:
Error 1004 "Application-defined or Object-defined error"
Look here for several potential issues/potential fixes:
VBA Runtime Error 1004 "Application-defined or Object-defined error" when Selecting Range
Use the VBA debugger and step through your macro a line at a time, until you find the specific object it's barfing on:
https://www.techonthenet.com/excel/macros/vba_debug2013.php
EDIT:
Having said that, I think Tim Williams's suggestion is probably spot-on:
You should always scope your Range/Cells calls with a worksheet
object, otherwise they will reference whatever happens to be the
Activesheet.
But PLEASE:
If at all possible, make the effort to learn troubleshooting tools available to you (like the debugger).
One other "useful tips" link I'd urge you to look at:
http://www.jlathamsite.com/Teach/VBA/WritingBulletProofCode.pdf
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(,))"
'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.