In a spreadsheet formula, =VALUE("$100") will evaluate to the numeric value of 100. I then tried to access this function in VBA via WorksheetFunction object, however it is missing.
In VBA I tried the conversion function Val("$100"), however that returns 0. So how can I accomplish this via VBA?
Val() only really works if the string is all numbers I'm afraid - currency signs cause it a problem.
If you're always going to have the same currency sign in the string, it might be worth using something like
StringName = replace(StringName, "$", "")
to take out the $ by replacing it with "" - otherwise if your strings aren't always going to be this predictable the below question might help:
How to find numbers from a string?
see https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.numbervalue
example of using above, which will return a value of -1234.56:
MsgBox WorksheetFunction.NumberValue("-$1,234.56", ".", ",")
Note that if the result is non-numeric, it throws an error. For example (swapping the comma grouping and decimal character params which is invalid in this case):
MsgBox WorksheetFunction.NumberValue("-$1,234.56", ".", ",")
I don't understand why the above link doesn't have any version info. It is currently dated 2019-05-23 - no idea if that's because it is new or if it was recently updated.
Related
I am trying to replace a comma with another field so you can import the data using a csv. I can do that successfuly by adding .ToString().Replace(","," And ") to the end of the field
EX. Fields!Fieldname.Value.ToString().Replace(","," And ")). This will replace the comma with the word And.
The issue I have encounterd is when the field is blank. It then exports a #Error in the cell. I have tried hiding the cell if it is blank but that works for everything but when it exports to a .csv.
If I cannot get the field to be just blank I would like to have it display 99.
Please help.
I have tried this as well...
=Iif(isNothing(Fields!FieldName.Value),"99",(Fields!FieldName.Value.ToString().Replace(","," And ")))
It still displayed an error
The problem is that you are trying to convert NULL (Nothing) to a string. Both sides of IIF are evaluated so even though you will never see the result, false part is failing when FieldName is null.
TO get round this, we first check if the field is null, then replace that with and emptry string "" and then do the ToString() bit.
try the following...
=IIF(
isNothing(Fields!TestField.Value),
"",
(IIF(IsNothing(Fields!TestField.Value),
"",
Fields!TestField.Value
).ToString().Replace(","," And "))
)
Hi I would like to extract dynamically the numbers from string in Excel.
I have the following strings and I would like to have only the numbers before ". pdf". taken out of the string into the next column.
As you can see the number of characters varies from line to line.
I have invented something like this:
=MID(M20;SEARCH("_";M20);20)
But this takes out only the numbers after "_" and .pdf after this....
How to make it the way I like?
D:\Users\xxxx\Desktop\1610\ts25b_4462.pdf
D:\Users\xxx\Desktop\1610\ts02b_39522.pdf
D:\Users\xxxxx\Desktop\1610\ts02b_except_39511.pdf
D:\Users\xxxx\Desktop\1610\ts02b_except_39555.pdf
D:\Users\xxxx\Desktop\1610\ts22b_6118.pdf
So that I have just :
4462
39522
39511
39555
6118
and so on...
Thank you!!!
With VBA, try to do it like this:
Public Function splitThings(strInput As String) As String
splitThings = Split(Split(strInput, "_")(1), ".")(0)
End Function
Concerning your formula, try to use =LEFT(MID(M20;SEARCH("_";M20);20),K), where K is the difference of the length of ts22b_6118.pdf and 4 (.pdf). 4 is the length of .pdf.
Something like this should do the work:
=LEFT(MID(I3,SEARCH("_",I3)+1,LEN(I3)),LEN(MID(I3,SEARCH("_",I3),LEN(I3)))-5)
You should do it using Excel formula. For example:
=SUBSTITUTE(LEFT(A1,FIND(".pdf",A1)-1),LEFT(A1,FIND("_",A1)),"")
Using the first line as an example, with LEFT(A1,FIND(".pdf",A1)-1) you will have D:\Users\xxxx\Desktop\1610\ts25b_4462 and with the LEFT(A1,FIND("_",A1)) D:\Users\xxxx\Desktop\1610\ts25b_, if you SUBSTITUTE the first part by "" you will have 4462.
Hope this can help.
With this formula, you should be able to get the numbers you want:
=MID(A1,FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))+1,FIND(".",A1)-FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))-1)
Basically, this is the initial fomula:
=MID(A1,FIND("_",A1)+1,FIND(".",A1)-FIND("_",A1)-1)
But since there may be two _ in the string so this is the one to find the 2nd _:
=SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_","")))
Now just replace this SUBSTITUTE with A1 above and you get that long formula. Hope this helps.
This will return the number you want regardless of extension (could be .pdf, could be .xlsx, etc) and regardless of the number of underscores present in the filename and/or filepath:
=TRIM(LEFT(RIGHT(SUBSTITUTE(SUBSTITUTE(M20,".",REPT(" ",LEN(M20))),"_",REPT(" ",LEN(M20))),LEN(M20)*2),LEN(M20)))
I'm looking to have the results of two If statements calculated and added in the same cell. I'm getting #VALUE! error.
=IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))+IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))
=IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))
+
IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))
Your both IF functions return an empty string "". Using + operator with strings returns #VALUE!. There are different methods to fix it:
use 0 instead of ""
use SUM instead of + (it ignores strings).
And as #John Bustos mentioned in his comment, you can simplify your formula with
IFERROR(value,value_if_error)
IFERROR description
If I am retrieving a value from a database which could be Null, will adding a "" to the value avoid the problem of the value potentially being Null?
Dim strFinal as String
strFinal = rsCustomers("Suburb") & ""
If rsCustomers("Suburb") happens to be Null, will the adding of "" to it cause the result to be a string?
I am looking at someone else's code and I am just trying to understand why there is the continual use of this pattern.
I have found exactly your question here Two handy functions for Null handling in "Tip of the Day"
You're probably aware that most VB functions don't work well with Null
values, which is an issue when you're working with database columns
that can accept Nulls. For example, the following statement:
Dim s as String
s = rs.Fields("AnyField")
can raise error 94 "Invalid use of Null". The usual workaround is to
force the conversion to string by appending an empty string, as in:
s = rs.Fields("AnyField") & ""
However, this solution slightly affect code readability, especially
if other people in your team aren't aware of the trick
I'm currently trying to create a report using Crystal Reports that comes with Visual Studio 2008.
I would like to include a field of type boolean on my report that shows a string rather than true or false. The string should contain either contain a € or a % sign.
How would I go about doing this in the Formula Workshop?
I've tried things like e.g.
if {tblAankoopDetails.SoortKorting} = true then "€" else "%"
However this never seems to work and results in warnings such as "The formula result must be a number".
This should be fairly simple but this is my first go at using Crystal Reports.
Help would be much appreciated.
Jay
Make sure your SoortKorting field has always true or false. Maybe there's a null and in that case your formula will not work.
Try with this:
if isnull({tblAankoopDetails.SoortKorting} ) then
" "
else
if {tblAankoopDetails.SoortKorting} =true
then "€" else "%"
Make sure there is nothing else in the same formula. Usually I see that particular error when a formula sometimes returns a string, and sometimes a number.
Also, you shouldn't need to test for true, so you might try:
if {tblAankoopDetails.SoortKorting} then "€" else "%"