CStr does not handle 3 decimals figures with comma - excel

I have the following issue.
When I try to do :
cell.value=CStr(cell.value)
it works with numbers like 6,91.
But when I try with numbers like 6,911 I get 6911 in return when I just want 6,911 instead.
I'm using commas because I'm in Europe, I guess maybe VBA mixes it up with the American way of writing thousands with a comma.
Indeed, here I only want a decimal with 3 figures after the comma

This does not what you expect it to do
cell.value=CStr(cell.value)
Here CStr(cell.value) will turn it into a String but if you write a string into a cell that looks like a number Excel "thinks" and turns it back into a number. Here comes the confusion.
If you want to format that cell as text use
Cell.NumberFormat = "#"
Cell.Value = Format$(cell.Value, "0.000")
or use Cell.Value = "'" & Format$(cell.Value, "0.000")

Related

Extract text from a long string of text

I need to extract the following portion CDA-CUP-PF from the following string of
MECH~CDA-CUP-PF~1 - CUP0915.2XL - Copper Reducer (P)
text
AddFormula TopLeft.Offset(1, 3).Resize(RowCount, 1), "=IFERROR(RIGHT(AA" & Row & ",FIND(""~"",AA" & Row & ")-1,FIND(""^"",AA" & Row & ")+1-FIND(""-"",AA" & Row & ")),"""")"
This is what I see right now: MECH^CHU
I need to see this: CDA-CUP-PF
I need to use something like the VBA code above.
Assuming your pattern is isolating the text in between ~ a formula solution is:
=MID(A1,FIND("~",A1)+2,FIND("~",A1,FIND("~",A1)+1)-FIND("~",A1)-3)
A VBA - UDF solution would look something like this
Public Function Isolate(x As Range)
Dim xString: xString = Split(x, "~")
Isolate = xString(1)
End Function
This formula will do what you want: =TRIM(MID(SUBSTITUTE(A1,"~",REPT(" ",255)),255,255))
It works by replacing ~ with 255 spaces, then it carves out 255 characters from 255 characters in (Which guarantees we get what you want) then it trims off the spare spaces.
If you want the other parts, use left or right instead of mid.
The UDF is a much better option though especially as you are doing this from code already.

How to convert numbers from European format to British in excel (separator commas to dots)?

I have excel files containing data in European format. Thousand separator is a dot, but I want to use a comma. I can easily convert it using =SUBSTITUTE function. However, when I do that I lose zeros at the end of the number.
For instance
11.500 (converts to) 11,5 (eleven thousand five hundred becomes eleven and a half)
After the conversion, I tried to change the custom format, but I had no luck.
Any ideas?
A). Using The Excel Formula
The problem you faced is due to the auto-conversion done by Excel to treat '.' as the DecimalSeparator. The below formula uses '&""' to force Excel to treat the value in the cell as the Text value instead in the Substitute function. The Value function will then force the result of the Substitute (in Text) to a number. You can then apply your desired NumberFormat accordingly.
=VALUE(SUBSTITUTE(<Cell Address>&"",".",""))
e.g.
=VALUE(SUBSTITUTE(A1&"",".",""))
B) Using The VBA code
You can use the below to force the system to revert to 'dot' as the decimal separator and ',' as the thousand separator.
Sub OverrideSystemSeparators()
Application.DecimalSeparator = "."
Application.ThousandsSeparator = ","
Application.UseSystemSeparators = False
End Sub
The above code assumes that the excel file is opened using the Europe system with default Decimalseparator (',') and Thousandseparator ('.').
However, if you open it using UK/US system and the data is still in Europe format, do validate if the data is in Number format or Text format.
If it is in Text format, do use the below code instead -- you will need to replace the last 2 statements based on the data range you would like to convert. In brief, it replaces '.' with nothing and then, applies the NumberFormat to add ',' as the thousand separator.
Sub OverrideSystemSeparators()
Application.UseSystemSeparators = True
ActiveCell.Value = Replace(ActiveCell.Value, ".", "")
ActiveCell.NumberFormat = "#,##0.00"
End Sub

How to add leading zeros in Excel (timestamp with ms has no leading zeroes)

I’m still having a grave problem with some files. It’s a rather stupid problem, but I’ve been working at it for quite some time and can’t find a solution.
I need leading zeroes in the time stamps, at least on the ms level.
The timestamps that my software makes always look like this: (example)
9:55:1:19 (that is 9h, 55min, 1sec, 19 ms)
while what I need would look like
09:55:01:019
It’s no problem to make a conversion in Excel. I use
=VALUE(SUBSTITUTE(G2;":";",";3))
but I always get
09:55:01:190 (190ms!!)
Thus the milliseconds are always read like comma values, which is understandable from the software’s point of view.
I'd like a solution that either appends the correct values to the end of each row in a new column or directly changes the values in the original column (D) to the correct values. (Appending is OK because my other scripts work that way already!)
Can you help out really quickly?
https://www.dropbox.com/sh/3ch6ikddplnyjgg/vUfnVgbbzH here's an example file
With a value in A1 like:
0.413206238
the formula:
=SUBSTITUTE(TEXT(A1,"hh:mm:ss.000"),".",":")
will display:
09:55:01:019
EDIT#1:
Or if you want to convert the values in column D, in place. Select the cells and run this small macro:
Sub FFormat()
Dim r As Range, L As Long, U As Long
For Each r In Selection
ary = Split(r.Text, ":")
U = UBound(ary)
L = LBound(ary)
For i = L To U
If Len(ary(i)) = 1 Then
ary(i) = "0" & ary(i)
End If
Next i
If Len(ary(U)) = 2 Then
ary(U) = "0" & ary(U)
End If
r.Value = Join(ary, ":")
Next r
End Sub
If the original in R12 (my example) is text, you can enter this big formula: :)
=TEXT(LEFT(SUBSTITUTE(R12;":";REPT(" ";20));4);"00") & ":"&TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));14;20);"00") & ":" & TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));34;20);"00") & ":" & TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));54;20);"000")
Depending on your regional settings you may need to replace field separator "; " by ","
Your data in column G has a leading space - this formula in a new column should convert to a valid time value whether you have leading spaces or not
=LEFT(TRIM(G2);FIND("^";SUBSTITUTE(TRIM(G2);":";"^";3))-1)+LOOKUP(1000;RIGHT(G2;{1;2;3})+0)/86400000
format as [h]:mm:ss.000
This will cope with single or double digit milliseconds, assumes that if there are no milliseconds you will still have third : followed by a zero. Also copes with single digit hours, minutes or seconds

String conversion with TEXT formula character limit?

I am attempting to format a single number stored as a text value.
For example, I would like to convert:
5145350002005000080
To:
5145-350002-00500-0080
The formula I am using is:
=text(A1,"0000-000000-00000-0000")
The output I am receiving is:
5145-350002-00500-0000
Why are the last 4 characters "0000" instead of "0080" as I would expect? Is there a character limit, or is my formula incorrect?
Quote from Large Number Arithmetic:
The limit in Excel is 15 significant digits in a number. Enter a 16
digit credit card number and 1234567890123456 will become
1234567890123450.
Actually, even 5145350002005001111 will result in 5145-350002-00500-0000.
Moreover, take a look at formula bar when your input cell is selected - for my Excel 2007 I see:
Hope that was helpful)
EDITED:
As a solution to solve the task - keep your numbers formatted as text and use the following formula:
=LEFT(A1,4)&"-"&MID(A1,5,6)&"-"&MID(A1,11,5)&"-"&RIGHT(A1,4)
Here is a custom function. Place it in a regular code module of the workbook and you can call it in the cell by =FormatLargeNumber("A1")
Public Function FormatLargeNumber(val As String)
'This function parses extremely large numbers per your example.
' Modify as needed.
FormatLargeNumber = Left(val, 4) & "-" _
& Mid(val, 5, 6) & "-" & _
Mid(val, 11, 5) & "-" & _
Right(val, 4)
End Function

When putting string that into Excel spreadsheet it puts to Scientific notation or rounds up

I am putting a string into excel. The string is often only numeric digits but can have alpha characters or hypens etc.
When I don't set the number format or set it like this
(Where xlSheet(0) is Excel.Worksheet)
xlSheet(0).Columns("N:N").EntireColumn.Columns.NumberFormat = "#"
It outputs in scientific notation.
When I use this code:
xlSheet(0).Columns("N:N").EntireColumn.Columns.NumberFormat = "0"
It rounds up the number to the nearest 100,000 so that the last five digits are 0's when they shouldn't be.
Should be: 1539648751235678942
But is: 1539648751235600000
The cells that have a hyphen or a letter aren't affected and work fine.
Any help would be greatly appreciated.
EDIT:
I add the data like this:
I loop through and put in xlSheet(0).Cells(i, 14) = rs!value_number
Where rs is my ADODB.Recordset
EDIT2: Herbert Sitz got it by adding an apostrophe before the text! Thanks everyone.
I think problem is that the number you're trying to enter can't be accommodated exactly by Excel. Excel has limitations on what numbers it display/represent because of the way numbers are stored internally. In Excel's case numbers are limited to 15 digit precision (see http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx ), which is not enough to represent your number.
You can enter the number as a string ("152..42") and all digits will be displayed, but you won't be able to perform exact mathematical operations with it.
For numbers, Excel can only handle 15 significant digits.
If you want to store a number that is more than 15 digits long without losing data, you have to store the data as text.
Doing what you've been doing will resolve the issue:
You can do either of the following to add your numbers as text:
xlSheet(0).Cells(i, 14).Numberformat = "#"
xlSheet(0).Cells(i, 14) = rs!value_number
Or
xlSheet(0).Cells(i, 14) = "'" & rs!value_number

Resources