can not set a formula in specific cell - vba

I trying to put a formula in my spreadsheet this formula works very well when i simply copy & paste in excel but when I try to create a macro for this some error occurs I cant understand what is the reason as it seems everything correct in my macro.
I am using below code-
Sub putformula()
range("K4").formula="=IF(ISBLANK($M4);"";IF($M4<=15;ABS($E4)*$K$3;""))"
range("L4").formula="=IF(ISBLANK($M4);"";IF($M4>31;"";IF($M4>15;ABS($E4)*$L$3;"")))"
range("M4").formula="=IF(OR($O4="ABS-Journal";$C4="ABS-Journal");"";IF(ISBLANK($D4);"";IF($C4=$O4;"";IF(MONTH($A4)=MONTH($M$3);IF(ISNUMBER(SEARCH("Invoice";$C4;1));$N4-$A4;$A4-$N4);""))))"
range("N4").formula="=IF(ISBLANK($B4);$N3;$A4)"
range("O4").formula="=IF(ISBLANK($B3);$O3;IF(ISBLANK($B3);"";$C4))"
End Sub
any suggestion will be appreciated.

Try this
Range("K4").Formula = "=IF(ISBLANK($M4),"" "",IF($M4<=15,ABS($E4)*$K$3,""""))"
You are also using ";" instead of "," .
The problem is you have to build a string and you were doing it wrong.
Try to understand and make the changes for every other range.

Related

VBA coding for Sumproduct() function with condition and named column range

Excel 2013
I am trying to use my excel sumproduct function in VBA but no luck.
I have two formulas one is without condition that work fine in VB but the other one with condition is not working. Here are the formulas
formula with no condition work fine in Excel
=SUMPRODUCT(tblProduct[Qty],tblProduct[amount])
and its VBA code that working fine in VBA excel userform
vGrndTot = Application.SumProduct(Range("tblProduct[Qty]"), Range("tblProduct[amount]"))
Conditional SUMPRODUCT working fine in excel
=SUMPRODUCT(--(tblProduct[prodID]="XYZ"),tblProduct[Qty],tblProduct[amount])
and its VBA code that i am tying to use but it not work (error type missmatch)
vTot = Application.SumProduct(--(Range("tblProduct[prodID]") = "XYZ"), Range("tblProduct[Qty]"), Range("tblProduct[amount]"))
any hint what I am doing wrong ?
I read so many question some said use evaluate (that too didn't work) some said use +0, or * 1 but i had no luck.
It took my 2 days but finally I got it with 2 key point
to evaluate the formula must appears EXACTLY SAME as in the worksheet cell including doublequotes. and to have doublequotes it need to have 4 doublequotes, strange though (see the vbDoubleQuote value below).
here is the final solution
' vProdID = coming from tblProdList one by one
vbDoubleQuote = """"
vFormula2Evalaute = "=SUMPRODUCT(--(tblProduct[prodID]=" & vbDoubleQuote & vProdID & vbDoubleQuote & ") ,tblProduct[Qty],tblProduct[UP])"
MsgBox Evaluate(vFormula2Evalaute)

Why is my auto fill not working?

Hoping you can help with the below, I've no idea why this isn't working and I can't seem to figure it out. After some Googling, I can't even find another example of this issue.
Essentially the code should take data on one page, place HLOOKUPS in another page to sort everything into the right columns (all working fine). Then once that's done, it should auto fill down using the row count.
The problem I have, is that it is auto filling on the wrong sheet (it might be worth calling out that the sheet it fills is the same one the code is in and where the rowcnt is.
I tried to explicitly call out the sheet I want to use as such: Range("A2:V2").AutoFill Destination:=Sheets(5).Range("A3:V" & rowcnt), Type:=xlFillDefault but this then throws an Application-defined or object-defined error on the fill line of code.
Public Sub FormatData()
rowcnt = Application.WorksheetFunction.CountA(Sheet4.Range("B:B")) + 1
With Sheets("Final Datasets")
.Cells(2, "A").FormulaR1C1 = _
"=HLOOKUP(""oOrder_date"",'Teradata Downloads'!R1:R1048576,ROW('Final Datasets'!RC),0)"
[snip] load more of the same as above [/snip]
'FILL
Range("A2:V2").AutoFill Destination:=Range("A3:V" & rowcnt), Type:=xlFillDefault
End With
End Sub
I'm properly puzzled here, so any help you can give as to how to fix this (and more importantly, why it's happening) would be greatly appreciated.
if you have the AutoFill in the With Statement, try to use the "." before the Range.
.Range("A2:V2").AutoFill Destination:=.Range("A3:V" & rowcnt)

VBA excel copy cell comment and past it onto a new sheet without using clipboard?

Hello my goal is to copy a comment, of a cell without having to use the clipboard (or all that added code :D) im still new to VBA so sorry for any errors :D
I was trying to use this code below it works great on cells but im not sure if it works on comments to
Sheets("Dump").Range("A1").Copy Destination:=Sheets("RR").Range("K1")
i try to change it around a bit but it gave me an error "application-defined or object-defined error"
Sheets("Dump").Range("A1").CopyComment Destination:=Sheets("RR").Range("K1").AddComment
ty for any help one can give
Here's an example using Cell A1 comment being copied to B1
Sub Testit()
Range("A9").Copy
Range("B9").PasteSpecial xlPasteComments
End Sub

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.

Using Excel Formula functions (ERF, ERFC) in Excel VBA code?

I'm trying to use the error function and the complimentary error function in my program. Neither are working. I keep getting the error Compile Error: Sub or Function not defined. However, if I go into a cell and manually try to use the error function, it works.
Why is that? How can I use it in my actual code?
The error function is ERF(x) and the complimentary error function is ERFC(x).
Here's an example of things that don't work:
Sub SeriouslyWHYIsntThisWorking()
x = 3
Range("A1") = Erf(x)
End Sub
Even this doesn't work:
Sub PleaseWork()
Range("A1") = Erfc(1)
End Sub
But if I went into Excel and typed =ERF(3) or =ERFC(1) into a cell, it'll work.
I'm very new to this and probably missing something incredibly simple. Help would be greatly appreciated!
Do you have the Analysis Toolpak for VBA add-in installed/referenced? (Look for atpvbaen.xls)
The ERF function is part of that add-in, and there are two versions of it (one for use in Excel functions, and one for VBA), and you'll need the right one set up and referenced by your project to be usable.
The add-ins are standard from MSFT, but not always set up by default. If you can use it in Excel normally, then you've already set up at least the Excel version. So using it all like it looks like you want to do will require the add-in, regardless of how you implement/use that function. Meaning, if you want to share this with anyone else, they will need the add-in installed/activated.
To link together this answer with the others provided (which are equally accurate and correct), either setting a cell value with
Range("A1").value = Application.WorksheetFunction.ERF(x)
or setting a cell formula with
Range("A1").Formula = "=Erfc(" + x + ")"
will require the end-user using the add-in.
To use a worksheet formula in vba, you need to put Application.WorksheetFunction. in front of it.
Some functions do have vba equivalents, but (as far as I know) not in the case of erf and erfc
Try this:
Sub ThisShouldWorkNow()
x = 3
formula = "=Erfc(" + x + ")"
Range("A1").Formula = formula
End Sub
Totally untested, since I don't have Excel on my Linux machine... But I think I'm getting the point across -- you need to use the .Formula property of the Range object.
There's more information here:
http://msdn.microsoft.com/en-us/library/office/gg192736.aspx