Excel Converting number incorrectly

Status
Not open for further replies.

Mister H

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

I am working on a spreadsheet and when I type in a long number Excel is converting the number. I try keying:

123123456123412345612345612341234

Excel populates the cell with: 1.23123456123412E+32 or futher shortens that to 1.23E+32

Can this feature be turned off. I am trying to get a range of cells to format that number as:

123-123456-1234-123456-123456-1234-1234

I have a code that was supplied to me but as mentioned above Excel is changing my number. I do not want to have to key ' before each number.

Thanks,
Mark
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi:

That works when I try it on a new sheet but it does not seem to work when I have formatting or code that is also being applied to that cell. If I format the column as text and then set the number format as:

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

The system then reverts back to converting my number to a formula? Maybe I am missing someting...

Thanks,
Mark
 
Upvote 0
When you apply a numeric format, yeah, I think you are corecing the cell back to number.

Could use change event code to enter the ' and format with dashes, but that seems a bit overkill ...
 
Upvote 0
I am uncertain on how to accomplish that as I do not write much code? I am still open to suggestions from the world... THANKS for your time.
 
Upvote 0
Hi:

Yes the data will always be the same length. However, I want the use to be able to stop entering when the remainder of the number is 0's

For example 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

As mentioned the formatting and the lenght of the number are constant. Hopefully this makes sense. If not let me know.

THANKS Again,
Mark
 
Upvote 0
This seems like it may work, but the range in question must first be formatted as Text.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
<SPAN style="color:#00007F">Dim</SPAN> Rng <SPAN style="color:#00007F">As</SPAN> Range, TempStr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, c <SPAN style="color:#00007F">As</SPAN> Range
<SPAN style="color:#00007F">Set</SPAN> Rng = [A1:D10] <SPAN style="color:#007F00">' Change A1:D10 to the range you want to monitor</SPAN>
<SPAN style="color:#00007F">If</SPAN> Intersect(Target, Rng) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
<SPAN style="color:#00007F">Const</SPAN> NUM_LEN = 33 <SPAN style="color:#007F00">' this is the length of your value, less the dashes</SPAN>
Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Rng = Intersect(Target, Rng)
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Rng
    <SPAN style="color:#00007F">With</SPAN> Application.WorksheetFunction
        TempStr = c.Value & .Rept("0", .Max(NUM_LEN - Len(c.Value), 0))
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    c.Value = Left$(TempStr, 3) & "-" & Mid$(TempStr, 4, 5) & "-" & Mid$(TempStr, 10, 4) _
                  & "-" & Mid$(TempStr, 14, 6) & "-" & Mid$(TempStr, 20, 6) & "-" & Mid$(TempStr, 26, 4) _
                  & "-" & Right$(TempStr, 4)
<SPAN style="color:#00007F">Next</SPAN> c
Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Rng = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
Hi:

I am trying to work with this code. I have a couple of questions. How do I add a second Range to this code? I have 2 Ranges to use this coding on.

Also, when I use this code it seems to work however, when I delete the number out of the cell it puts in

000-00000-0000-000000-000000-0000-0000 (it will not allow me to remove the 0's which poses a problem when I use my Delete Rows Macro).

Can this code be altered to leave the cell blank when it is:

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

THANKS for your assistance and time,
Mark
 
Upvote 0
Mister H said:
Hi:

I am trying to work with this code. I have a couple of questions. How do I add a second Range to this code? I have 2 Ranges to use this coding on.

Also, when I use this code it seems to work however, when I delete the number out of the cell it puts in

000-00000-0000-000000-000000-0000-0000 (it will not allow me to remove the 0's which poses a problem when I use my Delete Rows Macro).

Can this code be altered to leave the cell blank when it is:

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

THANKS for your assistance and time,
Mark

You *really* need to stay on a single post per topic.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
<SPAN style="color:#00007F">Dim</SPAN> Rng <SPAN style="color:#00007F">As</SPAN> Range, TempStr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, c <SPAN style="color:#00007F">As</SPAN> Range
<SPAN style="color:#00007F">Set</SPAN> Rng = Union([A1:D10], [X1:Z10]) <SPAN style="color:#007F00">' Change A1:D10 to the range you want to monitor</SPAN>
<SPAN style="color:#00007F">If</SPAN> Intersect(Target, Rng) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
<SPAN style="color:#00007F">Const</SPAN> NUM_LEN = 33 <SPAN style="color:#007F00">' this is the length of your value, less the dashes</SPAN>
Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Rng = Intersect(Target, Rng)
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Rng
    <SPAN style="color:#00007F">If</SPAN> Len(c.Value) > 0 <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">With</SPAN> Application.WorksheetFunction
            TempStr = c.Value & .Rept("0", .Max(NUM_LEN - Len(c.Value), 0))
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        c.Value = Left$(TempStr, 3) & "-" & Mid$(TempStr, 4, 5) & "-" & Mid$(TempStr, 10, 4) _
                      & "-" & Mid$(TempStr, 14, 6) & "-" & Mid$(TempStr, 20, 6) & "-" & Mid$(TempStr, 26, 4) _
                      & "-" & Right$(TempStr, 4)
    <SPAN style="color:#00007F">Else</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">Next</SPAN> c
Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Rng = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


</FONT>
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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