4 leading digit, Custom number format

hamncheese

New Member
Joined
May 8, 2018
Messages
5
Hi, does anyone know how to Custom Format number to "force" excel to start 4 digit from the beginning of number ?
This is the list of number that i have :
1607235967990.png


and i need to change them into this format :
1607235991370.png


Thank you for anyone willing to read and help me with this

Regards, Erik
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi Erik

For 11 digits you can set the format like this > "+"####"-"####"-"###
For 12 digits you can set the format like this > "+"####"-"####"-"####
For 13 digits you can set the format like this > "+"####"-"####"-"####"-"#
 
Upvote 0
hi, thank you for the help
i apologize for not being clearer with my question
i need a Custom Number Format Rule for 1 whole Column that contains several different digits
such in my example, in 1 column i have :
- 11 digits
- 13 digits
(there are other digits also such as : 12 and 14)

if im not mistaken, from what you propose 1 column can only have 1 type of digits

as for now i have to settle with this solution :
1607256578239.png
 
Upvote 0
Hi Erik,

This is all I have... but I am sure that experts out here will come up with a better option

Regards,

Humayun
 
Upvote 0
I doubt one number format rule can suit all different digits but you can write a VBA script to go through each cell, check the length of digits, and apply the proper number format assuming one digit length has only one format. Something like the following:

VBA Code:
Sub num_format()

Dim rng As Range
Dim cell As Range
Dim cell_length As Integer

Set rng = Range("A1:A4")

For Each cell In rng
cell_length = Len(cell)

Select Case cell_length

Case 11
cell.NumberFormat = "+####-####-###"

Case 12
cell.NumberFormat = "+####-####-####"
Case 13
cell.NumberFormat = "+####-####-####-#"
End Select

Next

End Sub
 
Upvote 0
Could use a custom function as well.

Book1
AB
1562224230473+6222-4230-473
166283822595688+6283-8225-9568-8
1762856200778+6285-6200-778
18628112237260+6281-1223-7260
Sheet6
Cell Formulas
RangeFormula
B15:B18B15=rx(A15)


VBA Code:
Function RX(s As String)
With CreateObject("VBSCript.RegExp")
    .Pattern = "((?:\d{4}))"
    .Global = True
    If .test(s) Then
        RX = "+" & .Replace(s, "$1-")
        If Right(RX, 1) = "-" Then RX = Left(RX, Len(RX) - 1)
    Else
        RX = "NA"
    End If
End With
End Function
 
Upvote 0
Hi, does anyone know how to Custom Format number to "force" excel to start 4 digit from the beginning of number ?
This is the list of number that i have :
1607235967990.png
I think we need to clarify... your cells are formatted as Text, correct? If they are formatted as Text, then your "numbers" are not really numbers and, hence, you cannot format the cells to display them differently. If they are not formatted as Text, then what are they formatted as and how are you getting so many digits into the cell without having the ending digits change to zeros?
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top