Formatting a cell for Numerical Value

Mister H

Well-known Member
Joined
Mar 6, 2002
Messages
1,507
Hi:

I am trying to customize some cells to display a number in a specific format. The format I want is as follows:

000-000000-0000-000000-000000-0000-0000

I tried to customize the the cell but end up with weird results. For example if a user types in 27123456 I want the formula o return:

027-123456-0000-000000-000000-0000-0000

The computer puts:

000-000000-0000-000000-000000-2712-3456


The number will ALWAYS start with 0 and end with 0000. Whatever number the user types in I want this info at the left hand side. ANy suggestions???

THANKS,
Mark
Sudbury , Ontario
 
Unfortunately the system is showing the following:

123-123456-1234-120000-000000-0000-0000
It appears that Excel, in its infinite wisdom, is converting the number to scientific notation, so it is dropping a bunch of the numbers at the end. I tried changing the cells I am entering into to Text, but it did not make a difference.

I am kind of stuck, I am really not to sure where to go from here.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Well THANKS for trying.. If anyone else has a suggestion for my formatting or a solution to the problem with the above code it would be greatly appreciated.

Bye 4 Now,
Mark :biggrin: :) :cool:
 
Upvote 0
Hi Lenze:

I will try and play around with the code you wrote for the others. Unfortunately I am not very good at writing code but I will see if I can muck my way through it. To clarify things I want the following:

If the user enter 1112222223333444444 I want them to tab out of the cell and then it populates with:

111-222222-3333-444444-000000-0000-0000

The formatting and the length of the number are constant. However I was just informed that on occasion this number may include a letter.


If the user enter 111222222A333444444 I want them to tab out of the cell and then it populates with:

111-222222-A333-444444-000000-0000-0000

Will your suggestion still work? Can this be applied only to 2 certain cell ranges? The areas I am trying to format are:

The 2 Ranges I have are:

MISC consists of $A$25:$G$35 the cells I want the formatting for would be E26 to E35 (this will change as more rows are added)

REV consists of $A$39:$E$49 the cells I want the formatting for would be E40 to E49 (this will change as more rows are added)
Hopefully this makes sense. If not let me know.

THANKS Again,
Mark
 
Upvote 0
What is happening here is not an Excel bug, but rather a computer science issue regarding Double Precision Floating Point numbers, with 15 maximum digits, a potential problem even if the cell is preformatted as text.

To get around that we can use VBA with a TextBox, or to keep things simple, an InputBox.

Two items I do not understand...

You wrote:
"I want the formula to return:
027-123456-0000-000000-000000-0000-0000
The computer puts:
000-000000-0000-000000-000000-2712-3456
The number will ALWAYS start with 0 and end with 0000."

Later in this thread, you wrote:
"Entered:
123123456123412345612345612341234 which should result in
123-123456-1234-123456-123456-1234-1234"

I do not understand why the latter scenario should result as you say, if there is no leading zero and no trailing 4 zeros like you first said.

I do not know what you would expect if more than 33 characters are entered (33 based on how you presented the layout, not including the dashes).



If the latter scenario (no leading zero) is really what you want

and

If you want the value to be truncated for the first 33 characters if more than 33 are attempted

then see if this helps.

Right click on your sheet tab, left click on View Code, and paste the following code into the large white area that is the worksheet module. Press Alt+Q to return to the worksheet.


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Option Explicit
Public RangeMisc As Range
Public RangeRev As Range

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

With Range("MISC")
Set RangeMisc = .Offset(0, 4).Resize(.Rows.Count, 1)
End With

With Range("REV")
Set RangeRev = .Offset(0, 4).Resize(.Rows.Count, 1)
End With

If Intersect(Target, RangeMisc) Is Nothing _
And Intersect(Target, RangeRev) Is Nothing Then
Set RangeMisc = Nothing: Set RangeRev = Nothing
Exit Sub

Else
Application.EnableEvents = False
Cancel = True

Dim TargAdd$, MyValRaw$, MyValFormat$
TargAdd = Target.Address(0, 0)

MyValRaw = _
InputBox("Enter your value for " & TargAdd & ":", _
"What do you want to enter into cell " & TargAdd & "?")
Select Case True
Case StrPtr(MyValRaw) = 0
MsgBox "You hit Cancel.", 48, "Entry cancelled for " & TargAdd
Case Len(MyValRaw) = 0
MsgBox "You hit OK but entered nothing.", 48, "Entry scuttled for " & TargAdd
Case Else
If Len(MyValRaw) < 33 Then
Dim MyValLen%
MyValLen = 33 - Len(MyValRaw)
MyValRaw = MyValRaw & WorksheetFunction.Rept("0", MyValLen)
End If

MyValFormat = _
Chr(39) & _
Left(MyValRaw, 3) & "-" & _
Mid(MyValRaw, 4, 6) & "-" & _
Mid(MyValRaw, 10, 4) & "-" & _
Mid(MyValRaw, 14, 6) & "-" & _
Mid(MyValRaw, 20, 6) & "-" & _
Mid(MyValRaw, 26, 4) & "-" & _
Mid(MyValRaw, 30, 4)

Target.Value = MyValFormat
End Select

Application.EnableEvents = True
Set RangeMisc = Nothing: Set RangeRev = Nothing
End If

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If IsEmpty(Target) Then Exit Sub

With Range("MISC")
Set RangeMisc = .Offset(0, 4).Resize(.Rows.Count, 1)
End With

With Range("REV")
Set RangeRev = .Offset(0, 4).Resize(.Rows.Count, 1)
End With

If Intersect(Target, RangeMisc) Is Nothing _
And Intersect(Target, RangeRev) Is Nothing Then
Set RangeMisc = Nothing: Set RangeRev = Nothing
Exit Sub

Else

Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Please double-click cell " & Target.Address(0, 0) & vbCrLf & _
"to enter a value into it.", 64, "DoubleClick needed as entry method"
Set RangeMisc = Nothing: Set RangeRev = Nothing
End If
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Note, this code will adjust itself for the named ranges if rows are inserted or deleted, which is also what I think you requested.

Also, I'd recommend instead of the Change event that you establish a data validation message box when a cell in column E of those named ranges is selected, so people will not enter in a long value only to be told later it must be entered with an inputbox. I added the Change event here anyway, just to cover our bases temporarily, or permanently if you want to keep it that way.
 
Upvote 0
Format the column as Text *before* entering data. Then, a modification of jmiskey's code will do the job. Also, the code will adapt itself to any changes to the definition of named ranges MISC or REV.
Code:
Option Explicit

 Private Sub Worksheet_Change(ByVal Target As Range)

    Dim myValue As String

    If Application.Intersect(Target, _
            Target.Parent.Columns(5), _
            Application.Union(Target.Parent.Range("MISC"), _
                Target.Parent.Range("REV"))) Is Nothing Then
    Else
        On Error Resume Next
        Application.EnableEvents = False
        myValue = Target.Value _
            & Left("000000000000000000000000000000000", 33 - Len(Target))
        'desired pattern: 000-000000-0000-000000-000000-0000-0000 _
         but may contain characters.  Hence, cannot use Format()
        Target.Value = Left(myValue, 3) & "-" _
            & Mid(myValue, 4, 6) & "-" _
            & Mid(myValue, 10, 4) & "-" _
            & Mid(myValue, 14, 6) & "-" _
            & Mid(myValue, 20, 6) & "-" _
            & Mid(myValue, 26, 4) & "-" _
            & Mid(myValue, 30, 4)
        Application.EnableEvents = True
        End If
   
    End Sub
Hopefully, the specifications will not evolve yet again. ;)

Mister H said:
Hi Lenze:

I will try and play around with the code you wrote for the others. Unfortunately I am not very good at writing code but I will see if I can muck my way through it. To clarify things I want the following:

If the user enter 1112222223333444444 I want them to tab out of the cell and then it populates with:

111-222222-3333-444444-000000-0000-0000

The formatting and the length of the number are constant. However I was just informed that on occasion this number may include a letter.


If the user enter 111222222A333444444 I want them to tab out of the cell and then it populates with:

111-222222-A333-444444-000000-0000-0000

Will your suggestion still work? Can this be applied only to 2 certain cell ranges? The areas I am trying to format are:

The 2 Ranges I have are:

MISC consists of $A$25:$G$35 the cells I want the formatting for would be E26 to E35 (this will change as more rows are added)

REV consists of $A$39:$E$49 the cells I want the formatting for would be E40 to E49 (this will change as more rows are added)
Hopefully this makes sense. If not let me know.

THANKS Again,
Mark
 
Upvote 0
THANKS to both of you. My post was a little confusing but I think you have nailed it. I will play around and see what happens. I have previously thought that the number would ALWAYS start and end with a 0 but I have been informed differently now.

Bye 4 Now,
Mark
 
Upvote 0

Forum statistics

Threads
1,216,489
Messages
6,130,959
Members
449,608
Latest member
jacobmudombe

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