Search code examples
excelvbadata-conversion

Convert 10M rows of Text with letters To Number


I need to convert "text numbers" into numbers. The text numbers look like: 32,23B or 242,23M.

I need to remove the letter and multiply by 1,000 ; 1,000,000 ; etc., depending on the letter.
The code below works, but as I need to convert 10M lines it takes ages.

Is there any clever way to do it?

For shIndex = startSheet To lastSheet
    LastRowF = Sheets(shIndex).Range("F" & Rows.Count).End(xlUp).Row
        
    For i = 3 To LastRowF
        
        Mul = Right(Sheets(shIndex).Range("F" & i), 1)
            
        Select Case Mul
            Case "K"
                Multiplier = 1
            Case "M"
                Multiplier = 1000
            Case "B"
                Multiplier = 1000000
            Case "T"
                Multiplier = 1000000000
            Case Else
                Multiplier = False
        End Select
            
        If Multiplier Then
            With Sheets(shIndex)
                .Range("F" & i) = CSng(Left(Sheets(shIndex).Range("F" & i), Len(Sheets(shIndex).Range("F" & i)) - 1) * Multiplier)
                .Range("F" & i).NumberFormat = "#,##0"
            End With
        End If
    Next i
Next shIndex

Solution

  • Use a Variant array, do the conversion in memory, then write the entire array back to the sheet:

    For shIndex = startSheet To lastSheet
        LastRowF = Sheets(shIndex).Range("F" & Rows.Count).End(xlUp).Row
    
        Dim data() As Variant
        data = Sheets(shIndex).Range("F3:F" & LastRowF).Value
    
        Dim i As Long
        For i = LBound(data, 1) to UBound(data, 1)
            Mul = Right(data(i, 1), 1)
    
            Select Case Mul
                Case "K"
                    Multiplier = 1
                Case "M"
                    Multiplier = 1000
                Case "B"
                    Multiplier = 1000000
                Case "T"
                    Multiplier = 1000000000
                Case Else
                    Multiplier = False
            End Select
    
            If CBool(Multiplier) Then
                data(i, 1) = Left(data(i,1), Len(data(i, 1)) - 1) * Multiplier
            End 
    
            With Sheets(shIndex).Range("F3:F" & LastRowF)
               .Value = data
               .NumberFormat = "#,##0"
            End With 
         
        Next
    Next