I'm trying to insert formulas into cells using this following code:
ActiveCell.Formula = "=ISERROR(IF(O" & ActiveCell.Row & "=M; _
LEFT(O" & ActiveCell.Row & ";LEN(O" & ActiveCell.Row & ")-1)*1000000; _
LEFT(O" & ActiveCell.Row & ";LEN(O" & ActiveCell.Row & ")-1)*1000000000);" & vide & ")"
(vide is a string variable) but it keeps returning Application defined or Object defined error.
Thanks !
Change ; to ,
Change " & vide & " to """ & vide & """
Change M to ""M""
Change ISERROR to IFERROR
ActiveCell.Formula = "=IFERROR(IF(O" & ActiveCell.Row & "=""M"",LEFT(O" & ActiveCell.Row & ",LEN(O" & ActiveCell.Row & ")-1)*1000000,LEFT(O" & ActiveCell.Row & ",LEN(O" & ActiveCell.Row & ")-1)*1000000000),""" & vide & """)"
It's because of your local settings, you use ; unstead of ,.
If you must use ;, then try replacing:
ActiveCell.Formula
with:
ActiveCell.FormulaLocal
Also, I like to use Chr(34) to get the ".
So at the end change your formula to:
...1000000000);" & Chr(34) & vide & Chr(34) & ")"
Related
For i = 2 To 50
ws.Range("K" & i).FormulaLocal = "=if(countif($C$2:C" & i & ";C" & i & ")=1;row();"")"
Next i
ws.Range("K" & i).FormulaLocal = "=if(countif($C$2:C" & i & ";C" & i & ")=1;row();"")
I am getting error on this line. Where am I going wrong?
you need to double the quotation marks within the string (at the end):
ws.Range("K" & i).FormulaLocal = "=if(countif($C$2:C" & i & ";C" & i & ")=1;row();"""")"
after running the code it is showing the text like this "AASD FDG HJK LSDH YTWIN " in a cell but i need those words in a cell wrapped
like
AASD
FDG
HJK
LSDH
YTWIN
this in one cell. what changes needs to be done to get like that for this code
Ws.Range("E" & R).Value = "AASD FDG HJK LSDH YTWIN "
To have a line break in an Excel cell, use a LineFeed character. In VBA, a constant vbLf is defined for that. Furthermore, the cell needs to have the WrapText-property set.
So you can use
With ws.Range("E" & R)
.WrapText = True
.Value = "AASD" & vbLf & "FDG" & vbLf & "HJK" & vbLF & "LSDH" & vbLF & "YTWIN"
' Or use
.Value = Replace("AASD FDG HJK LSDH YTWIN ", " ", vbLf)
end With
Your question is answered here
In your case, the solution would be:
Ws.Range("E" & R).Value = "AASD" & chr(10) & "FDG" & chr(10) & "HJK" & chr(10) & "LSDH" & chr(10) & "YTWIN "
As mentioned in linked post, this automatically sets WrapText to True.
I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"
I have data in column A, C, E and G. I want column I to hold all of the data from these separated by semicolons, I have searched the web but all I find is how to replace line breaks with semicolons and that doesn't do it. Below are the 3 pieces of code I have attempted to work into it
Range("I" & i).Value = "=A" & i & Chr(59) & "&" & "C" & i & "&" & "E" & i & "&" & "G" & i
Range("I" & i).Value = "=A" & i & "&C" & i & "&E" & i & "&G" & i
Range("I" & i).Value = Range("A" & i).Value + ; + Range("C" & i).Value + ; + Range("E" & i).Value + ; + Range("G" & i).Value
The second line comes closest to what I want but when I add & ";" into it I get errors.
Any ideas thoroughly appreciated
SOLVED : This was solved by an answer below, I had managed my own fix which I will detail but the accepted answer is a cleaner, more efficient way
mystring = "=A" & i & "&" & Chr(34) & ";" & Chr(34) & "& C" & i & "&" & Chr(34) & ";" & Chr(34) & "& E" & i & "&" & Chr(34) & ";" & Chr(34) & "& G" & i
Range("I" & i).Value = mystring
You should use "&"";""".
In detail, you should use something like
"=A" & i & "&"";""" & "&C" & i
which result in
=A1&";"&C1
suppose i = 1.
=CONCATENATE(A1, ";", C1, ";", E1, ";", G1)
Using VBA
Range("A2").FORMULA = REPLACE("=CONCATENATE(A1, ';', C1, ';', E1, ';', G1)", "'", CHR(34))
I want to make a formula with a & inside it but VBA thinks its a concatenate symbol
""&"" does not work, are there any other tricks?
My formula:
Sheets("Elasticity").Cells(iRow, 38).Formula = "=SUMIFS(" & "All_Models!$W$2:$W$" & nrow & ",All_Models!$G$2:$G$" _ & nrow & ",Elasticity!L" & iRow & ",All_Models!$AL$2:$AL$" & nrow & ",Elasticity!AK" & iRow & _ ",All_Models!$B$2:$B$" & nrow & "," & "" <= "" & "&" & "ElasticityA" & iRow & ")"
I want to transform:
"" <= "" & "&" & "Elasticity!A" & iRow & ")"
into:
"<="&Elasticity!A2)
How about just this:
dim s as string
s = "my_complicated_formula" & "&" & "and_another_formula"
...All_Models!$B$2:$B$" & nrow & ",""<=""&" & "Elasticity!A" & iRow & ")"
s="foo" & Chr(38) & "bar"
Where chr(38) = &