how do I format currency inside the expression of Excel cell? - excel

I am trying to figure out how to format the result in an expression in Excel. I wrote
=IF(C30 > B30, "You would save $" & Format(C30-B30, "Currency") & "!", "No savings")
inside the cell but it doesn't work. Simply put, I want the currency formatted inside the expression.
How?

Have you tried the Text function?
=IF(C30 > B30, "You would save " & Text(C30-B30, "$0.00") & "!", "No savings")

Use this formula
=IF(C30 > B30, "You would save " & Currency(C30-B30, 0) & "!", "No savings")

Many years later, this works, too!
=IF(C4>D4,"You would save " & DOLLAR(C4-D4,2) & "!","No Savings!")

I think you are looking for the Concatenate function. Excel doesn't seem to have the Format function you have indicated.
This worked for me in Excel 2007:
=IF(C30 > B30, CONCATENATE("You would save $",C30-B30, "!"), "No savings")

Related

Expression.Error when dynamically passing in Teradata SQL query (with column aliases) to ODBC query

I have a macro that prompts me for a SQL query (unless it was called by another Sub, in which case it uses the argument that was passed into its optional string parameter as the query) and then executes the query against my Teradata SQL database.
It works fine, unless there's a column alias containing a space in the query.
Example query:
SELECT 2 + 2 AS "Query Result";
Error:
Run-time error '1004':
[Expression.Error] The name 'Source' wasn't recognized. Make sure it's spelled correctly.
The line of code which I believe is the culprit is as follows (my apologies for the readability-- I recorded the macro, modified it just enough to get it to work somewhat dynamically and then haven't touched it since).
ActiveWorkbook.Queries.Add Name:=queryName, formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=my-server-name"", " & Chr(34) & code & Chr(34) & ")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
I assume it has to do with the fact that the example query above has double quotes for the alias, which are confusing the syntax when trying to be interpolated. Any help would be greatly appreciated!
Here's what the string for the formula is set to in this line:
ActiveWorkbook.Queries.Add Name:=queryName, formula:=<string here>
after all the chr() and concatenation are done:
let
Source = Odbc.Query("dsn=my-server-name", "<code>")
in
Source
That token <code> is replaced by whatever is in your variable code. So I suspect you are correct in that this formula would need to have it's double quotes escaped fully.
In other words this string you are building form Formula is going to be evaluated as code itself, and even in that evaluation it will be passing more code (your SQL) onto the Teradata server to be evaluated there.
You are in code inception. VBA code writing powerquery code writing Teradata code.
Understanding that and guessing a bit here, I'm thinking your current code variable looks something like:
code="SELECT 2 + 2 AS ""Query Result"";"
Your double quotes are already escaped for VBA. BUT because you have to survive another round of eval in powerquery you need to escape once again. Instead:
code="SELECT 2 + 2 AS """"Query Result"""";"
*I think...

If cell is blank, what to do?

I have line of code in excel and would like to add a function that If reference cell is empty, just return me empty. How am I able to add to that? Thanks in advance!
Here My code is so far:
=IF(
ISNA(VLOOKUP($A2,'[DataSheet.xlsx]SOC 41'!$C$2:$P$5000,1,FALSE))=TRUE,"","R41: " & VLOOKUP($A2,'[DataSheet.xlsx]SOC 41'!$C$2:$P$5000,10,FALSE))&"
"&IF(ISNA(VLOOKUP($A2,'[DataSheet.xlsx]SOC 42'!$C$2:$P$5000,1,FALSE))=TRUE,"","R42: " & VLOOKUP($A2,'[DataSheet.xlsx]SOC 42'!$C$2:$P$5000,10,FALSE))&"
"&IF(ISNA(VLOOKUP($A2,'[DataSheet.xlsx]SOC 43'!$C$2:$P$5000,1,FALSE))=TRUE,"","R43: " & VLOOKUP($A2,'[DataSheet.xlsx]SOC 43'!$C$2:$P$5000,10,FALSE))
Again, what if return cell got an empty string, how to add ISBLANK (or something else) in to the code?
Even I tried this and didn't work.
=IF(OR(
VLOOKUP($A11,'[DataSheet]SOC 41'!$C$2:$P$5000,11,FALSE)="",
VLOOKUP($A11,'[DataSheet]SOC 42'!$C$2:$P$5000,11,FALSE)="",
VLOOKUP($A11,'[DataSheet]SOC 43'!$C$2:$P$5000,11,FALSE)=""),"",
IF(
ISNA(VLOOKUP($A11,'[DataSheet]SOC 41'!$C$2:$P$5000,1,FALSE))=TRUE,"","R41: " & VLOOKUP($A11,'[DataSheet]SOC 41'!$C$2:$P$5000,11,FALSE))&"
"&IF(ISNA(VLOOKUP($A11,'[DataSheet]SOC 42'!$C$2:$P$5000,1,FALSE))=TRUE,"","R42: " & VLOOKUP($A11,'[DataSheet]SOC 42'!$C$2:$P$5000,11,FALSE))&"
"&IF(ISNA(VLOOKUP($A11,'[DataSheet]SOC 43'!$C$2:$P$5000,1,FALSE))=TRUE,"","R43: " & VLOOKUP($A11,'[DataSheet]SOC 43'!$C$2:$P$5000,11,FALSE)))
Please help!
Nest the formula in:
=if(A2<>"",formula,"")

currency conversion:number to words excel

I would like to convert the the Kuwait currency which has three decimal places into words in an Excel Document.The formula i used is as follows:
=if(or(isBlank($E$89),not(isNumber($E$89)),$E$89>=power(10,15)),ifError(1/0),trim(arrayFormula(concatenate(if(trunc(mod($E$89,power(10,15,12,9,6,3}))/power(10,{12,9,6,3,0}))<100,"",switch(int(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0}))/100),1," one",2," two",3," three",4," four",5," five",6," six",7," seven",8," eight",9," nine") & " hundred") & if(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100)<>0,if(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0}))>100," and",if($E$89>power(10,{15,12,9,6,3}),switch({1,2,3,4,5},5," and"),"")),"") & if(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,12,9,6,3,0})),100)=0,"",if(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100)<20,IFERROR(switch(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100),1," one",2," two",3," three",4," four",5," five",6," six",7," seven",8," eight",9," nine",10," ten",11," eleven",12," twelve",13," thirteen",14," fourteen",15," fifteen",16," sixteen",17," seventeen",18," eighteen",19," nineteen")),IFERROR(switch(int(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100)/10),2," twenty",3," thirty",4," forty",5," fifty",6," sixty",7," seventy",8," eighty",9," ninety")) & if(mod(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,12,9,6,3,0})),100),10)=0,"","-" & IFERROR(switch(mod(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100),10),1,"one",2,"two",3,"three",4,"four",5,"five",6,"six",7,"seven",8,"eight",9,"nine"))))) & if(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0}))=0,"",IFERROR(switch({1,2,3,4,5},1," trillion",2," billion",3," million",4," thousand"))))) & if($E$89>=2," Dinar only",if($E$89>=1," Dinar only","")) & if((round($E$89-trunc($E$89),3)*1000=0)+($E$89<1),""," and") & switch(trunc(round($E$89-trunc($E$89),3)*1000,-2)/100,1," one-hundred",2," two",3,-hundred" three-hundred",4," four-hundred",5," five-hundred",6," six-hundred",7," seven-hundred",8," eight-hundred",9," nine-hundred"& if(round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-2)<20,switch(round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-1),0,"",1," one",2," two",3," three",4," four",5," five",6," six",7," seven",8," eight",9," nine",10," ten",11," eleven",12," twelve",13," thirteen",14," fourteen",15," fifteen",16," sixteen",17," seventeen",18," eighteen",19," nineteen"),switch((round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-2))/10,2," twenty",3," thirty",4," forty",5," fifty",6," sixty",7," seventy",8," eighty",9," ninety") & if(mod(round($E$89-trunc($E$89),3)*1000,10)=0,"","-" & switch(round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-1),0"",1,"one",2,"two",3,"three",4,"four",5,"five",6,"six",7,"seven",8,"eight",9,"nine"))) & " fils only")))
The error shown is
Formula parse error
For a given input:
111.250
i would expect to get the output as :
One hundred and eleven Dinar and two hundred fifty fils only.
Try looking for JavaScript solutions to use then as custom function.
I've found is this code, provided by #Vilas Shetkar:
https://stackoverflow.com/a/37409158/5372400
I've tested this code and it works in sheets, so you may adopt this or other similar solutions to your needs.

Enter special characters in Excel VBA

How do I enter special characters in Excel VBA? For example if I want "●" (large interpunct) in some strings, they all appear as "?". I tried to substitute all "●" with ● in VBA but it didn't work, simply displayed ● in the string.
Reference Website - Unicode and VBA’s ChrW() and AscW() functions
Here the code for bullet et strong bullet. Use hexadecimal in lieu of long.
Sub DisplayBullet()
Cells(1, 1).Value = "This is a bullet - " & ChrW(8226)
Cells(2, 1).Value = "This is a strong bullet - " & ChrW(&H25CF)
End Sub
Important: If you try to display in a MsgBox it's not working.

vba Using Eval but maintaing internal string

So I am using Instr with Evaluation and facing some difficulties
The code is as follows
myIneq=">"
myString1="Hello"
myString2="el"
Evaluate( "Instr(" & myString1 & "," & myString2 & ")" & myIneq & cstr(0)
I am getting an Error 2029. Based off this msdn link I am assuming it is trying to evaluate "Hello" as a variable name. What is the work around for this, I know there must be one.
Thanks
I infer from the Error 2029 (#NAME?) and the link that you're using Excel. In this case the answer is simple. Application.Evaluate evaluates Excel expressions, not VBA code. That is, any functions you call in your expression have to be things you could call from an Excel formula. (And you're correct that Excel is trying to evaluate the value of a symbol it doesn't recognize, and is thus giving you back a #NAME? error.)
There is an Excel worksheet function, FIND, that does pretty much the same thing that the VBA function Instr does, so if your example is not too simplified, that might be all you need to do.
I just typed this into the Immediate window:
x="Hello"
y="el"
?Evaluate("FIND(""" & y & """, """ & x & """)")
2
ineq = ">"
?Evaluate("FIND(""" & y & """, """ & x & """)" & ineq & "0")
True
and it seems to work.
Note that Evaluate is a function, so it expects to receive a string argument, and then return what that string evaluates to if treated as an Excel formula-syntax expression. In your example, you don't seem to be doing anything with the return value, so I thought I'd mention it.
"Evaluate" doesn't understand all excel functions.
For example, trying to evaluate "instr" will give an Error 2029. But there is a nice workaround:
"evaluate" recognizes all added vba functions of your excel sheet
so just wrap a single-line function around the reluctant function.
Code will be similar to this:
Sub test()
MsgBox Evaluate(" Instring(""Hello"",""el"") ")
Msgbox "\o/ ! ... I owe a beer to someone out there"
End Sub
Function Instring(a, b)
'make instr visible to 'evaluate'
Instring = InStr(a, b)
End Function
You're evaluating the string InStr(Hello,el).
Obviously, that's not what you want.
You need to use a quoted string literal.

Resources