String conversion with TEXT formula character limit? - excel

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

Related

CStr does not handle 3 decimals figures with comma

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")

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.

Extract and add sub strings into strings

The context here is that I'm trying to identify phone number patterns of a big messy column, and format them as this:
(CC) NNNN-NNNN
CC being two digit area code
There may be two or more numbers in the same cell(They need to remain in the same cell unfortunately), and need to be as follow :
(CC) NNNN-NNNN / (CC) NNNN-NNNN
The numbers are just raw digits, no spaces or characters, but are as TEXT and need to remain so because of the 15 digit limit on Excel
Now, I'm having problem with two cases
Case 1:
Two phones and one area code (18 digits)
Example : CCNNNNNNNNNNNNNNNN
What I need is a function that takes the first two characters from this string and add them at the eleventh spot of said string, resulting in a 20 char string
Case 2:
One phone and two same area code(12 digits)
CCCCNNNNNNNN
This one just need to remove the first two characters
Tried this way for case 1, but ended up with a 22 string char? not sure where I went wrong
s1 = Mid(Cells(j, 3), 1, 2)
s2 = Mid(Cells(j, 3), 3, 10)
s3 = Mid(Cells(j, 3), 11, 18)
s4 = s1 & s2 & s1 & s3
The big caveat here is the formatting of the existing data.... how do you know what digits are "area code" and what digits are phone number? You can do what you are trying to do here with Excel formulas without any VBA... For Case 1 you can use the formula below, replacing "A1" with the appropriate cell reference.
="("&MID(A1, 1, 2)&") "&mid(A1,3,4)&"-"&mid(a1,7,4)&" / ("&MID(A1, 1, 2)&") "&mid(A1,11,4)&"-"&mid(a1,15,4)
For Case 2 you can use the formula below.
="("&MID(A1, 1, 2)&") "&mid(A1,5,4)&"-"&mid(a1,9,4)
Does your solution have to be in vba?
The third parameter in Mid is the length of the extracted substring, not its final index. If it is omitted - the selection goes to the end of the string. The selection also goes to the end of the string if the length parameter exceeds the number of possible characters. s2 = Mid(Cells(j, 3), 3, 10) was thus of length 10 rather than the expected 8. Your case 1 would look something like this:
Sub test()
Dim s As String, t As String
s = "CCNNNNNNNNNNNNNNNN"
t = Mid(s, 1, 10) & Mid(s, 1, 2) & Mid(s, 11)
Debug.Print t
End Sub
Which has output:
CCNNNNNNNNCCNNNNNNNN
For your two cases:
=IF(LEN(A1)=18,REPLACE(A1,11,0,LEFT(A1,2)),IF(LEN(A1)=12,MID(A1,3,10)))
Not knowing the other formats or desired results, it is hard to expand on the formula. The formula will return FALSE if the length is other than 12 or 18.

Split Cell by Numbers Within Cell

I have some fields that need to be split up into different cells. They are in the following format:
Numbers on Mission 21 0 21
Numbers on Mission 5 1 6
The desired output would be 4 separate cells. The first would contain the words in the string "Numbers on Mission" and the subsequent cells would have each number, which is determined by a space. So for the first example the numbers to extract would be 21, 0, 21. Each would be in its own cell next to the string value. And for the second: 5, 1, 6.
I tried using a split function but wasn't sure how to target the numbers specifically, and to identify the numbers based on the spaces separating them.
Pertinent to your first case (Numbers on Mission), the simple solution could be as shown below:
Sub SplitCells()
Const RowHeader As String = "Numbers on Mission"
Dim ArrNum As Variant
ArrNum = Split(Replace(Range("A1"), RowHeader, ""), " ")
For i = 1 To UBound(ArrNum)
Cells(1, i + 2) = ArrNum(i)
Next
Cells(1, 2) = RowHeader
End Sub
The same logic is applicable to your second case. Hope this may help.
Unless I'm overlooking something, you may not need VBA at all. Have you tried the "Text to Columns" option? If you select the cell(s) with the information you would like to split up, and go to Data -> Text to Columns. There, you can choose "delimited" and choose a space as a delimiter, which will split your data into multiple cells, split by where the space is.
edit: Just realized that will also split up your string. In that case, when you are in 3rd part of the Text to Columns, choose a destaination cell that isn't the cell with your data. (I.E. if your data is in A1, choose B1 as destination, and it'll put the split info there. Then just combine the text columns with something like =B1&" "&C1&" "&D1)
I was able to properly split the values using the following:
If i.Value Like "*on Mission*" Then
x = Split(i, " ")
For y = 0 To UBound(x)
i.Offset(0, y + 1).Value = x(y)
Next y
End If

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

Resources