Custom number (price) format independent of localization - excel
I am wondering is it possible to have custom number format using Excel formula that will not be dependent on localization of Excel application (EU/US)?
For example I have value 1291660.
Then using formula =TEXT(A1;"# ##0,00"). I get as an output 1 291 660,00. The target is to have in any case 1.291.660,00 as an output. Any Excel professional to give an advice?
I have tried =TEXT(A1;"#.##0,00") - This didn't work
I think VBA is the only solution to this. I have found my old question about the same topic, but it seems that solution provided is not working for some reason?
Ultimate 1000 separator using VBA
Function CustomFormat(InputValue As Double) As String
Dim sThousandsSep As String
Dim sDecimalSep As String
Dim sFormat As String
sThousandsSep = Application.International(xlThousandsSeparator)
sDecimalSep = Application.International(xlDecimalSeparator)
' Up to 6 decimal places
sFormat = "#" & sThousandsSep & "###" & sDecimalSep & "######"
CustomFormat = Format(InputValue, sFormat)
If (Right$(CustomFormat, 1) = sDecimalSep) Then
CustomFormat = Left$(CustomFormat, Len(CustomFormat) - 1)
End If
' Replace the thousands separator with a space
' or any other character
CustomFormat = Replace(CustomFormat, sThousandsSep, " ")
End Function
By replacing CustomFormat = Replace(CustomFormat, sThousandsSep, " ") with CustomFormat = Replace(CustomFormat, sThousandsSep, ".") output is .1 291 660
You may use:
=SUBSTITUTE(SUBSTITUTE(FIXED(A1,2,0),",","."),".",",",INT(LEN(A1)/3)+1)
The way it works is that on an EU-system FIXED() will return: 1.291.660,00 but on an US-system it should return 1,291,660.00. To create the same output-string, we can SUBSTITUTE() all comma's to dots. A 2nd SUBSTITUTE() will then replace only the last dot back to a comma. To find the right index I used INT(LEN(A1)/3)+1 which works well on itegers like 1291660. If you happen to have decimal values, you can change this to:
=SUBSTITUTE(SUBSTITUTE(FIXED(A1,2,0),",","."),".",",",INT(LEN(INT(A1))/3)+1)
EDIT:
The above should always return the desired format, but it's a string. To return the numeric value in any further calculations, you can use NUMBERVALUE():
=NUMBERVALUE(C1,",",".")
Go to excel file tab, click options and then the following options as desired
Uncheck use system separators and define your own
You don't need VBA for this. You can use SUBSTITUTE to replace the default separator characters, and you can detect what these are by cutting them out from the formatted string of a known number. I use ASCII 1 (SOH) character to avoid replacing twice (e.g. replacing thousands separator from " " to ".", than replacing decimal separators from "." to "," would cause that thousands separators appear as ","):
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(1234567.89,"# ##0.000"),MID(TEXT("# ##0",1000),2,1),CHAR(1)&" "),MID(TEXT("0.0",0.1),2,1),CHAR(1)&","),CHAR(1)&" ","."),CHAR(1)&",",",")
This will output "1.234.567,890".
This output will appear as a string (you cannot add numbers to it, and it is left adjusted by default), and you cannot change this behavior if you don't use Excels local settings for separators.
BTW, using " " for thousands separator and either "." or "," for decimals is the clearest way of displaying numbers.
Related
Automatically convert fixed length strings into delimited strings
I have many files containing such lines : HUIHOJ OPKKA LK ASOIJS AISJJ PL AOSKSI ASIJD YA I want to convert theses lines into something like this : HUI;HOJ ;OPKKA ;L;K ASO;IJS ;AISJJ ;P;L AOS;KSI ;ASIJD ;Y;A So the first field would be 3 characters, second would be 4, third 6, fourth 1 and fifth 1 character. I know that it's possible to do it manually with excel, but I need to have automatically conversion solution, because I have many files with exactly the same structure.
VBA has a simple command to format text, so you can do this fairly easily, and with just a single line of code Function SpFmt(S As String) As String SpFmt = Format(S, "###\;####\;######\;#\;#") End Function If you want to use a worksheet function, you can do this with a nested replace formula on the worksheet: =REPLACE(REPLACE(REPLACE(REPLACE(A1,4,0,";"),9,0,";"),16,0,";"),18,0,";")
Use the VBA Join Function after splitting (Split function) on a space or simply replace (Replace functoin) all of the spaces with a space & semi-colon. dim str as string str = range("A1").value2 'HUIHOJ OPKKA LK range("A1") = Join(Split(str, char(32)), chr(32) & chr(59)) 'HUIHOJ ;OPKKA ;LK str = range("A1").value2 'HUIHOJ OPKKA LK range("A1") = Replace(str, chr(32), chr(32) & chr(59)) 'HUIHOJ ;OPKKA ;LK
If you are not bounded to Excel, you can use unix tools (also available for Windows) to do this very efficiently with just one command: cut --output-delimiter=";" -c 1-3,4-7,8-13,14,15 fixed.txt > delimited.csv The same command in a loop: for f in *.txt ; do cut --output-delimiter=";" -c 1-3,4-7,8-13,14,15 "${f}" > "${f}.csv" done Edit : the output delimiter option does not seem to work on every platform. Alternatively, you can use sed : sed "s/^\(.\{3\}\)\(.\{4\}\)\(.\{6\}\)\(.\)\(.\)/\1;\2;\3;\4;\5/" fixed.txt > delimited.csv
Finding multiple instance of a variable length string in a string
I'm trying to extract my parameters from my SQL query to build my xml for an SSRS report. I want to be able to copy/paste my SQL into Excel, look through the code and find all instances of '#' and the appropriate parameter attached to it. These paramaters will ultimately be copied and pasted to another sheet for further use. So for example: where DateField between #FromDate and #ToDate and (BalanceFiled between #BalanceFrom and #BalanceTo OR BalancdField = #BalanceFrom) I know I can use Instr to find the starting position of the first '#' in a line but how then do I go about extracting the rest of the parameter name (which varies) and also, in the first two lines of the example, finding the second parameter and extracting it's variable lenght? I've also tried using the .Find method which I've been able to copy the whole line over but not just the parameters.
I might approach this problem like so: Remove characters that are not surrounded by spaces, but do not belong. In your example, the parentheses need to be removed. Split the text using the space as a delimiter. For each element in the split array, check the first character. If it is "#", then the parameter is found, and it is the entire value in that part of the array. My user-defined function looks something like this: Public Function GetParameters(ByRef rsSQL As String) As String Dim sWords() As String Dim s As Variant Dim sResult As String 'remove parentheses and split at space sWords = Split(Replace(Replace(rsSQL, ")", ""), "(", ""), " ") 'find parameters For Each s In sWords If Left$(s, 1) = "#" Then sResult = sResult & s & ", " End If Next s 'remove extra comma from list If sResult <> "" Then sResult = Left$(sResult, Len(sResult) - 2) End If GetParameters = sResult End Function
Separate words with commas in Excel 2010
I'm trying to use a formula in Excel to separate a bunch of words in a cell with a comma. If there are more than 5 words in the cell, I just want to get the first 5 words. To get the first five words in a cell and separate them by a comma I use this: =SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",5))-1), " ", ", ") This works fine. But the problem with this, because of the number 5 here, if I a cell contains less than 5 words, I get an error. I tried to substitute the 5 with this: LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1 So my function becomes this: =SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1))-1), " ", ", ") But this doesn't work, it gives me an error. Any idea how I can do this please? Also I would like to ignore the first word if its first character is "-" (without the quotes) and just start from the second word. So in other words, I want something like this: I love my life very much should return I, love, my, life, very - I love my life very much should return I, love, my, life, very (the "-" is ignored") I love my should return I, love, my Thanks in advance for any help
Here's a somewhat different approach. Aside from the "less than 5" issue, it also deals with the "5 words with no space at the end" issue: =LEFT(A1,FIND("^",SUBSTITUTE(A1 & "^"," ","^",5))-1) EDIT 1: I just noticed the part about the leading "- ". My addition isn't very elegant, but it deals with it, and also TRIMS any trailing spaces: =TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1)) EDIT 2: Oh yeah, commas: =SUBSTITUTE(TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1))," ",",")
Try this: =TRIM(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-"," "))," ",","),",",REPT(" ",99),5),99))
This will work even if there is not a space after the dash or if there are extra spaces in the text. Often I find that input is not very clean. =SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)), " ","*",5),IFERROR(FIND("*",SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)), " ","*",5))-1,999))," ",",") Edit: After commenting on István's, I made mine flawless too. =SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-"," ",1) &MID(TRIM(A1),2,999))," ","*",5),IFERROR(FIND("*",SUBSTITUTE( TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-","",1)&MID(TRIM(A1),2,999))," ","*",5))-1,999))," ",",") But I think his is more elegant.
Try this: =SUBSTITUTE(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)),FIND("|",SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)))-1),",,",",") The formula works by taking the following steps: Remove any leading dash-space Trim any leading or trailing spaces Insert comma-spaces in place of spaces and add a trailing comma-space Calculate the lesser of 5 and the number of words in the string Put in "|" in place of either the fifth comma-space or the trailing comma-space if the string is less than five words Determine the position of the "|" Strip off the "|" and all characters to the right of it Remove any doubled commas due to any single embedded commas in the initial string If you are willing to consider a VBA solution, this complex expression can be replaced by a user-defined function: Function words5(InputString As String) As String Dim wordArray As Variant wordArray = Split(Trim(Replace(InputString, _ 'remove "-", put words into array "-", "", , 1)), " ") ReDim Preserve wordArray(LBound(wordArray) To _ 'drop all but the first 5 words WorksheetFunction.Min(UBound(wordArray), 5 - 1)) words5 = Replace(Join(wordArray, ", "), ",,", ",") 'rejoin the words with ", " End Function 'separator On the plus side of using this code is its maintainability compared to the worksheet formula, which impossible to understand or safely alter without access to the original building blocks that were combined into the single expression. The code would have to be installed in the workbook in which it is used or in either the standard Personal.xlsb workbook or an addin workbook. To use the function, copy and paste it into a standard module, which can be inserted into a workbook via the VBA editor. You can open the editor with the Visual Basic button on the `Developer tab of the ribbon.
Figured I'd throw my hat in the ring also. I think this formula should cover the bases: =SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1&" ","- ",""))," ",REPT(" ",99)),99*5))," ",",")
Replace String Two Different Parts
I am extracting a column of data from a range of filenames. All my filenames are strings in the form: Temporary PSD Report 'Month' 2011.xls I am using Replace to extract the month from each, at the moment I am doing it in two stages which works but it seems a bit clumsy. Is there a way to use some kind of AND for multiple replacements in the same string? Dim strfilename As String Dim mnth As String Dim mnthshrt As String mnth = Replace(strfilename, "Temporary PSD Report ", "") mnthshrt = Replace(mnth, " 2011.xls", "") I've tried using & and AND to reference both parts to be removed but it either has no effect on the original string or produces an error.
You could also split the string at each space character and take the 4th word (index starts at 0): s = "Temporary PSD Report 'Month' 2011.xls" mth = Split(s, " ")(3)
Converting unicode fraction characters (vulgar fractions) to decimals in Excel 2003
I'm trying to convert the following text to a decimal number in excel 2003: "93⅛" The output should be: 93.125 I've gotten this to work with ¼, ½, ¾ by using the Replace function in VBA: For example, this works: cur_cell = Replace(cur_cell, "½", " 1/2") However, the ⅛ and family characters are not supported in the VBA editor. They display as ??. Instead, I tried to replace the unicode value directly: cur_cell = Replace(cur_cell, " & ChrW$(&H215B) & ", " 1/8") But this doesn't work. Is there a good way to convert these strings to numbers that I can use?
The correct syntax is: cur_cell = Replace(cur_cell, ChrW$(&H215B), " 1/8") Your example was saying: replace the string consisting of a space, an ampersand, a space [etc.] with 1/8. Clearly that's not what you want to do! I'd actually recommend: cur_cell.Value = Replace(Replace(cur_cell.Value, ChrW$(&H215B), ".125")," ","") to circumvent Excel's automatic replacement of fractions. I just don't like to rely on that kind of automatic stuff. Why not write it as a decimal number straight off? Also, I like explicitly refering to the cell's .Value property as opposed to relying on it being the default property.