Need to keep leading zero's when saving to .CSV in Microsoft 365 MSO

Anonymous
2021-04-30T18:14:19+00:00

I am using Microsoft Excel for Microsoft 365 MSO and have a list of part numbers that are numbers stored as text with leading zeros.  When I save as .CSV for file import, the leading zeros disappear.  I need the .CSV file to retain the leading zeros.  I have tried reformatting as text with no success.  I have tried using the Convert Text to Columns Wizard with no success. I tried formula =LEFT(A2,LEN(A2)) and it worked until I need to copy and paste values, then it pastes it as Number stored as Text. Please help me figure this out.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

12 answers

Sort by: Most helpful
  1. Kevin Jones 7,225 Reputation points Volunteer Moderator
    2021-04-30T18:31:50+00:00

    This will work with copy/paste. When pasting, select Paste Special:

    Select Values and Add:

    This will paste the text values as numeric values.

    Kevin

    0 comments No comments
  2. Kevin Jones 7,225 Reputation points Volunteer Moderator
    2021-04-30T19:03:19+00:00

    You stated two problems in your original post. The first was exporting to a CSV while keeping leading zeroes. You stated you had a solution for that using a formula. The next problem you stated was "I tried formula =LEFT(A2,LEN(A2)) and it worked until I need to copy and paste values, then it pastes it as Number stored as Text." My solution solves that by converting the text values to numeric values. If you want the leading zeroes then you don't have any problems at this time. If you want the numbers stored as numbers then use my solution for pasting.

    If you want numbers stored as numbers to have leading zeroes on the worksheet then you have to format those cells appropriately. Here is an example of a number with six positions, five of them are leading zeroes:

    Kevin

    1 person found this answer helpful.
    0 comments No comments
  3. Kevin Jones 7,225 Reputation points Volunteer Moderator
    2021-04-30T19:06:07+00:00

    You can also solve the first problem using formatting. When numbers are formatted as illustrated in my previous post, saving the worksheet as a CSV file preserves the formatting.

    Kevin

    0 comments No comments
  4. Anonymous
    2021-04-30T18:32:02+00:00

    Try a macro to retain the formatting of the cell as a TXT file rather than a CSV: When you open the file, you will be able to use the text file dialog to specify that column being pulled in as text (which should preserve the leading 0s), which you can't do with a CSV file.  Of course, if you are not using Excel to re-open the file, change the .txt extension to .csv and the program you are using may keep the leading 0s.

    Sub ExportCurrentSheetToTXT()

        Dim fName As String

        Dim WholeLine As String

        Dim FNum As Integer

        Dim RowNdx As Long

        Dim ColNdx As Long

        Dim StartRow As Long

        Dim EndRow As Long

        Dim StartCol As Long

        Dim EndCol As Long

        fName = "C:\Excel\ExportText.txt"  'use a folder that exists

        Application.ScreenUpdating = False

        On Error GoTo EndMacro:

        FNum = FreeFile

        With ActiveSheet.UsedRange

            StartRow = .Cells(1).Row

            StartCol = .Cells(1).Column

            EndRow = .Cells(.Cells.Count).Row

            EndCol = .Cells(.Cells.Count).Column

        End With

        Open fName For Output Access Write As #FNum

        For RowNdx = StartRow To EndRow

            WholeLine = ""

            For ColNdx = StartCol To EndCol

                If WholeLine = "" Then

                    WholeLine = Cells(RowNdx, ColNdx).Text

                Else

                    WholeLine = WholeLine & "," & Cells(RowNdx, ColNdx).Text

                End If

            Next ColNdx

            Print #FNum, WholeLine

        Next RowNdx

        Close #FNum

    EndMacro:

        On Error GoTo 0

        Application.ScreenUpdating = True

    End Sub

    2 people found this answer helpful.
    0 comments No comments
  5. Anonymous
    2021-04-30T18:46:25+00:00

    Thank you for the response. It does, but it removes the leading zeros, which I'm needing to keep. And if there is data set that is not a number formatted as text, it does not paste. So I don't think this is the solution to my problem.

    0 comments No comments