Search code examples
exceltextnumbersfixed-length-record

Excel: Convert mass text to a number and maintain fixed length?


I'm working with about a 1 million record data set. Currently the data is formatted as the "text" type and I want it to be a "number" type.

I have used the copy special > multiply technique, but the conversion will drop leading zeros in the data set. I do not want this, I want to maintain the fixed lengths of the data inclusive of leading zeros.

How do I do this?


Solution

  • You can use VBA code like the one below, but you still need to insert the logic that will format the i-th column with the right length.

    Sub doIt()
    
        Dim i As Long
    
        Application.ScreenUpdating = False
        For i = 1 To 10 'replace 10 by the index of the last column of your spreadsheet
            With Columns(i)
                .NumberFormat = String(.Cells(2, 1), "0") 'number length is in second row
            End With
        Next i
        Application.ScreenUpdating = True
    
    End Sub