字符串转换差异

时间:2018-10-08 12:34:48

标签: excel string numbers double string-conversion

我要将几张数据导入另一个工作簿,并希望有一种简单的方法将数字字符串转换为数字。所以我发现这段代码完全符合我想要的https://www.thespreadsheetguru.com/the-code-vault/2014/8/21/convert-numbers-stored-as-text

它运行迅速且非常流畅,这一点非常令人赞赏,但是当我开始使用数据时,我发现了差异。我发现两个单元格已转换为两个完全不同的数字。在撰写本文时,这是我发现的仅有的两个,但令人担忧的是还会有更多。一个示例是字符串“ 1,225”变成了-611779。 (是的,我使用的是十进制逗号)

由于某种原因,它决定这两个数字“字符串”完全不同。但是,另一张纸上的相同数字已正确转换。

我现在的问题是:是否有某些原因导致这两个(可能还有更多的单元格)导致脚本完全无法正确转换这些数字。还是代码有缺陷?

Sub CleanData(sRange As Range)

'PURPOSE:Clean up selected data by trimming spaces, converting dates,
'and converting numbers to appropriate formats from text format
'AUTHOR: Ejaz Ahmed (www.StrugglingToExcel.Wordpress.com)
'SOURCE: www.TheSpreadsheetGuru.com/The-Code-Vault

Dim MessageAnswer As VbMsgBoxResult
Dim EachRange As Range
Dim TempArray As Variant
Dim rw As Long
Dim col As Long
Dim ChangeCase As Boolean
Dim ChangeCaseOption As VbStrConv
Dim rng As Range

'User Preferences
  ChangeCaseOption = vbProperCase
  ChangeCase = False

'Set rng = Application.Selection
Set rng = sRange

'Warn user if Range has Formulas
  If RangeHasFormulas(rng) Then
    MessageAnswer = MsgBox("Some of the cells contain formulas. " _
      & "Would you like to proceed and overwrite formulas with values?", _
      vbQuestion + vbYesNo, "Formulas Found")
    If MessageAnswer = vbNo Then Exit Sub
  End If

'Loop through each separate area the selected range may have
  For Each EachRange In rng.Areas
    TempArray = EachRange.Value2
      If IsArray(TempArray) Then
        For rw = LBound(TempArray, 1) To UBound(TempArray, 1)
          For col = LBound(TempArray, 2) To UBound(TempArray, 2)
            'Check if value is a date
              If IsDate(TempArray(rw, col)) Then
                TempArray(rw, col) = CDate(TempArray(rw, col))

            'Check if value is a number
              ElseIf IsNumeric(TempArray(rw, col)) Then
                TempArray(rw, col) = CDbl(TempArray(rw, col))

            'Otherwise value is Text. Let's Trim it! (Remove any extraneous spaces)
              Else
                TempArray(rw, col) = Application.Trim(TempArray(rw, col))

                'Change Case if the user wants to
                  If ChangeCase Then
                    TempArray(rw, col) = StrConv( _
                    TempArray(rw, col), ChangeCaseOption)
                  End If
              End If
          Next col
        Next rw
      Else
        'Handle with Single Cell selected areas
          If IsDate(TempArray) Then 'If Date
            TempArray = CDate(TempArray)
          ElseIf IsNumeric(TempArray) Then 'If Number
            TempArray = CDbl(TempArray)
          Else 'Is Text
            TempArray = Application.Trim(TempArray)
              'Handle case formatting (if necessary)
                If ChangeCase Then
                  TempArray = StrConv(TempArray, ChangeCaseOption)
                End If
          End If
      End If

    EachRange.Value2 = TempArray

  Next EachRange

'Code Ran Succesfully!
'MsgBox "Your data cleanse was successful!", vbInformation, "All Done!"

End Sub

------------------------------------------------------------------------
Function RangeHasFormulas(ByRef rng As Range) As Boolean

'PURPOSE: Determine if given range has any formulas in it
'AUTHOR: Ejaz Ahmed (www.StrugglingToExcel.Wordpress.com)
'SOURCE: www.TheSpreadsheetGuru.com/The-Code-Vault

Dim TempVar As Variant

TempVar = rng.HasFormula

'Test Range
  If IsNull(TempVar) Then
    'Some of cells have fromulas
      RangeHasFormulas = True
  Else
    If TempVar = True Then
      'All cells have formulas
        RangeHasFormulas = True
    Else
      'None of cells have formulas
        RangeHasFormulas = False
    End If
  End If

End Function

1 个答案:

答案 0 :(得分:1)

该代码的问题在于,VBA IsDate函数将使用逗号作为分隔符。因此1,225被认为是日期1-Jan-225。由于该值不是合法的Excel值,因此它将转换为负数(在1-Jan-1900之前)。

如果您要做的只是将存储为字符串的数字转换为实数,则可以使用:

Option Explicit
Sub colaTextToNumbers()
    Dim R As Range

'Can be set in many different ways
Set R = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp)) 'for column A

'Set R = Selection
'Set R = whatever

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

With R
    .EntireColumn.NumberFormat = "General" 'or could limit this just to R, not entire column
    .Value = .Value
End With

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With

End Sub