I am trying to insert a formula into a macro, but when I run the macro, it gets stuck at the formula. This is the format I'm using:
ThisWorkbook.Sheets("Sheet1").Range("B5").Formula = "=IF(ISERROR(ROUND(AVERAGE(D16:D75),3)), "", ROUND(AVERAGE(D16:D75), 3))"
The problem is with the quotes because they mess up with the string. Try replacing them with CHR(34)
ThisWorkbook.Sheets("Sheet1").Range("B5").Value = "=IF(ISERROR(ROUND(AVERAGE(D16:D75),3))," & CHR(34) & CHR(34) & ", ROUND(AVERAGE(D16:D75), 3))"
Eventually you can use doubled quotes (""""), but I find that quite difficult to read.
Related
I don't know the syntax for handling double quotation marks within a formula.
Here is my code:
ActiveCell.Formula = "=IF($W9="A",1,IF($W9="B",2, IF($W9="C",3,0))))"
Any help is greatly appreciated.
You need to change the formula to
ActiveCell.Formula = "=IF($W9=""A"",1,IF($W9=""B"",2, IF($W9=""C"",3,0)))"
So, first you need to double quote and in your case you also need to count the number of "(" and ")"
You need to double quotes for them to appear correctly in the formula
So: ActiveCell.Formula = "=IF($W9=""A"",1,IF($W9=""B"",2, IF($W9=""C"",3,0))))"
should give you the following formula in the ActiveCell:
=IF($W9="A",1,IF($W9="B",2, IF($W9="C",3,0))))
What I typically do is use the chr function in vba. You give the function the ASCII code for the desired character, this case 34, and vba fills it in for you.
ActiveCell.Formula = "=IF($W9=" & chr(34) & "A" & chr(34) & ",1,IF($W9=" & chr(34) & "B" & chr(34) & ",2, IF($W9=" & chr(34) & "C" & chr(34) & ", 3,0)))"
Cybernetic.nomad's answer works well, but in my experience (and mainly with Access) being super explicit gets the point across for the application, even though readability is practically non-existent.
I would posit that the best solution would to make the if statements in vba (instead of using excel functions) and return the value to the active cell. Then double quotes shouldn't be an issue at all in this case.
I have a need to pull a HTML string from an Excel sheet and update a variable in that string with a current value before sending as an email.
Let me explain with an example:
I have a worksheet that has a cell (A1 & B1) with values:
IMEI: " & Me.IMEI & "<br>
Name: " & Me.FullName & "<br>
In VBA I'm creating a String which will become the body of the email:
Body = Body & Worksheets("Worksheet").Range("A1").Value<br>
Body = Body & Worksheets("Worksheet").Range("B1").Value<br>
Now, in VBA, Me.IMEI and Me.Fullname are correct values that have been pulled from a Userform (e.g. "12.345678.876543.2" and "User, Test") and I want to update the strings with the actual values, rather than - as happens now - Me.IMEI and Me.Fullname are inserted in the body.
Or, is there a better way altogether to do this that doesn't require hardcoding the text of the email in the macro? (I have to get the macro signed by our central office and want the ability to change the text of the email that is created without having to get it resigned everytime that happens).
If I am understanding the comments correctly, and Me.IMEI is a user input on a userform, could you not just write the values of your hypothetical Cells A1 & B1 dynamically by using the following?
Sheet1.Cells(1,1).Value = "IMEI: " & Chr(34) & " & " & Me.IMEI.Value & " & " & Chr(34) & "<br>"
Depending on what userform control Me.IMEI is and what you are trying to extract, you might need Me.IMEI.value or .caption or .text
Ended up, at this point, just using a series of REPLACE() statements to catch all the tags that I might want to use. Seems like a fairly awful way of doing it, but it's still more dynamic than having the email text hardcoded (and thus uneditable) in the macro itself. Hopeful I can come up with a more elegant solution for my next release.
I am trying to get my VB script to enter an Excel formula into a cell. Unfortunately I had no luck so far.
Sheets("ABC").Select
Range("B2").Value = "=IF(Mat_Location!F2=0;"";Mat_Location!F2)"
After some googling and searching on this site I figured it would be because of the quotation marks and so I tried changing the quotation marks to double quotation marks.
Sheets("ABC").Select
Range("B2").Value = "=IF(Mat_Location!F2=0;"""";Mat_Location!F2)"
That also didn't help.
Any help is appreciated. Thanks in advance.
Replace your multiple " with Chr(34), it's easier to debug it this way.
Also, there's no need to Select the sheet first.
Sheets("ABC").Range("B2").FormulaLocal = "=IF(Mat_Location!F2=0;" & Chr(34) & Chr(34) & ";Mat_Location!F2)"
Note: it seems your Excel's regional settings is having ; as a delimeter inside the formula. The default settings is , , so for my Excel setting (maybe also yours) it might be:
Sheets("ABC").Range("B2").Formula = "=IF(Mat_Location!F2=0," & Chr(34) & Chr(34) & ",Mat_Location!F2)"
I've tried a variety of concats and uses of "" but I cant get .formula to work in the following code. The macro runs without error but it does not populate my sheet. I feel like this: Excel VBA formula with variables is what I am going for but main_row does not appear to be getting assigned to the variable when it is inside the .formula.
main_row = main.Range("b6").Row
calc_col = main.Range("k6").Column
main.Cells(main_row, calc_col).Formula = " = j & main_row / i & main_row "
Is it possible to use .formula with cells object?
Would rather not use application.worksheetfunction because i want end user to see the formula
Anyone have a good source to explain the proper way to qualify variables within .formula?
Try the below, I believe you were nearly there, but you inserting an unnecessary space before the equals sign + the quotes were out.
main.Cells(main_row, calc_col).Formula = "= J" & main_row & " / I" & main_row
I'm having trouble with the usage of Indirect function.
Here's what i'm looking for, I'm trying to create a dynamic vlookup based on the current tab.
=VLOOKUP(B3;'NH BBC'!$E$1:$Z$188;MATCH("Share Outstanding";'NH BBC'!$E$1:$Z$1;0);0)
My plan is to modify the 'NH BBC' by 'NH ' & RIGHT(CELL("filename");3) Supposing that the name of my tab is XXX_BBC.
I've tried to use indirect function but I'm not sure I'm on the good way.
Here's what I've tried:
=VLOOKUP(B3;INDIRECT("'" "NH " & "RIGHT(CELL("'" & "filename" & "'" & ");3)" & "!" & "E1:Z188");MATCH("Share Outstanding";'NH BBC'!$E$1:$Z$1;0);0)
Hope I've been clear.
Thanks in advance !
You are trying to concatenate some text with the results returned from a formula, but you are sticking the formulas in quotes, turning them into text. Furthermore, you are not keeping very good track of your text. There are quotes all over the place. Take this bit by bit in a seperate cell if need, slowly growing your formula from the inside out so you can insure everything is as expected. Right now it's a mess.
INDIRECT("'" "NH " & "RIGHT(CELL("'" & "filename" & "'" & ");3)" & "!" & "E1:Z188")
Should be:
INDIRECT("'NH " & RIGHT(CELL("filename");3) & "'!E1:Z188")
No need for all the complication.
I've finally found and this formula is working perfectly.
VLOOKUP($B3;INDIRECT("'NH "&RIGHT(CELL("filename");3)&"'!$G$1:$ZZ$9999");MATCH("SHARE_OUTSTANDING";INDIRECT("'NH "&RIGHT(CELL("filename");3)&"'!$G$1:$ZZ$1");0))
By the way the issue i've got is that the cell are changing when i'm using the formula in another tab. Is this possible to look the value i've obtained ?
Something like a F9 ?