"checked" statement causing error on a vba macro for excel - vba

So i need to add a formula to an entire column using a macro,
so far i have:
Range("S2:S" & lastRow).Formula = "=IF(TRIM(N2)="checked",M2,IF(LEN(P2)>=60, P2, IF(LEN(SUBSTITUTE(R2,CONCATENATE(" ", $J$2),""))>=60,SUBSTITUTE(R2,CONCATENATE(" ", $J$2),""),R2)))"
Which is supposed to take several values of different cells and form a new one, the problem is that the code sends an error "Syntax Error" but if i put the formula in a cell in excel the formula works perfectly.
Any idea on what could be causing the error? i think it could be linked with how its writen and how vba reads it.

In VBA you must double-up all the quotes inside, viz (there might be other issues, I haven't checked). I expect a google search would have solved this for you.
Range("S2:S" & lastRow).Formula = "=IF(TRIM(N2)=""checked"",M2,IF(LEN(P2)>=60, P2, IF(LEN(SUBSTITUTE(R2,CONCATENATE("" "", $J$2),""""))>=60,SUBSTITUTE(R2,CONCATENATE("" "", $J$2),""""),R2)))"

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)

Long formula works when put in cell but not when inserted from VBA

Firstly, this is a long and ugly formula warning.
What I am trying to do is to get VBA to insert this formula into Excel. If I get VBA to insert it without the '=' and then manually add it in Excel, it proves the formula works. Yet if I try to get VBA to insert and run the formula, with the '=' prefix, all in one go then I get a run-time error.
Any ideas how I can get VBA to insert and run this formula?
ActiveCell.FormulaR1C1 = _
"=IF(P1=""EQS FW"",IF(Statistics!B3=""Cadmium"",IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$5,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$5),'Hardness-bands'!$D$5,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$6,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$6),'Hardness-bands'!$D$6,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$7,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$7),'Hardness-bands'!$D$7,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$8,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))" & _
"<'Hardness-bands'!$C$8),'Hardness-bands'!$D$8,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$9,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$9),'Hardness-bands'!$D$9,""Err""))))),IF(Statistics!B4=""Copper"",IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$13,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$13),'Hardness-bands'!$D$13,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$14,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$14),'Hardness-bands'!$D$14,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$15,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300," & _
"MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$15),'Hardness-bands'!$D$15,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$16,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$16),'Hardness-bands'!$D$16,""Err"")))),IF(Statistics!B4=""Zinc"",IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$20,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$20),'Hardness-bands'!$D$20,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$21,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$21),'Hardness-bands'!$D$21,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-ban" & _
"ds'!$B$22,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$22),'Hardness-bands'!$D$22,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$23,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$23),'Hardness-bands'!$D$23,""Err"")))),Statistics!R4))),Statistics!R3)"
Wow, that is pretty big. You definitely ought to break that up a fair amount.
Anyway, I think it might be because you are trying to insert speech marks into your string. In VBA debug mode, copy the entire string and have a look at it in the immediate window. Is it exactly the same as the string when in an Excel cell? My guess would be not.
Try replacing the double speech marks:
P1=""EQS FW""
with something like this:
P1=" & """" & "EQS FW" & """"
That will insert the speech marks correctly for you.
Another thought. It will be tricky doing the speech mark adjustments, so start by trying it out on a small section of the vast formula and build it up gradually.

Writing formula into an Excel Range with Option Strict On

Is it possible to write formulas across a range in Excel from VB.Net? I'm using a String array to hold a list of formulas that I would like to apply to an Excel range, instead of looping through and writing them one at a time.
This line is what I am attempting to use to write to a range:
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).Formula = formulas
The formula is being written to Excel, but Excel is actually displaying the formula, instead of the calculated value. If I write each formula to each cell like this:
xlWorkSheet.Range("AF" & intCurrentRow.ToString).Formula = formulas(0)
xlWorkSheet.Range("AG" & intCurrentRow.ToString).Formula = formulas(1)
It works perfectly fine and Excel displays the calculated values as it should. Almost seems like I'm missing a step but I haven't been able to find anything in my research.
As soon as I hit post I figured out the problem. Instead of using the .Formula property of an Excel.Range, you have to use the .FormulaArray property instead.
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).FormulaArray = formulas

repost: copy pasting data instead of formula excel VBA

ok i have to repost this as my original thread was tagged as a duplicate with "the original" thread not having anything to do with my issue and the person who tagged it as a dupe cannot provide the answer.
there's a reason why i'm using activecell in my code as it does something with interaction with outlook and validation of certain conditions from other cells.
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1)
now i tried :
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1).pastespecial
but i'm getting error 1004.
if someone will point me here: how to paste only values in excel
please tell me which of the 4 answers will actually work, because none of them is working on my end.
Reverse the procedure and pass over the value directly.
wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1) = ActiveCell.Cells(1, 37).value
The .Value is the raw underlying value in a cell or the result from a formula if the cell contains a formula. .Value2 strips the value further where a date like 18-Feb-2015 becomes 42,053. If you have used number formatting in the cell (e.g. 18-Feb-2015) and wish to retain it, use .Text in place of .Value.

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.