【问题标题】:VBA: Convert Text to NumberVBA:将文本转换为数字
【发布时间】:2021-12-23 20:13:47
【问题描述】:

我有一列数字,无论出于何种原因,都被格式化为文本。这使我无法使用算术函数,例如小计函数。将这些“文本数字”转换为真数字的最佳方法是什么?

以下是具体问题的截图:

我尝试了这些 sn-ps 无济于事:

Columns(5).NumberFormat = "0"

 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

【问题讨论】:

  • 嗨 Scott - 我同意这是一个很好的解决方案。但是,我现在可以听到最终用户抱怨“一切都归零!!”已经。由于 VBA 已经被用于运行脚本(我应该在问题中提到这一点),我认为最好只包含代码以避免这种对话。
  • 使用 CInt(Cell().Value) 获取单元格的数值为数字
  • 我遇到了类似的问题,货币价值是由 0,00 雷亚尔 BRL 形成的,而 CCUR() 那天真的让我很开心。我认为 Cdec() 会有所帮助。检查链接上的其他功能。 [如何使用CDEC函数(VBA)](techonthenet.com/excel/formulas/ccur.php)
  • 我知道已经有一段时间了,但您输入值的方式可能不正确。如果你这样做:" cell.Value = "865" " 单元格将使用文本而不是数字来完成。如果您不使用“ cell.value = 865 ”,则单元格将以数字结尾。

标签: excel vba formatting


【解决方案1】:

使用以下函数(将[E:E]更改为您需要的适当范围)来规避此问题(或更改为任何其他格式,例如“mm/dd/yyyy”):

[E:E].Select
With Selection
    .NumberFormat = "General"
    .Value = .Value
End With

附:根据我的经验,与使用“警告框”方法相比,此 VBA 解决方案在大型数据集上的运行速度明显更快,并且使 Excel 崩溃的可能性更小。

【讨论】:

  • 在某些工作表上,这填充了整个列,而它最初只包含 200 行。 TextToCOlumn 方法似乎更可靠
  • 谢谢,这在我修改为不使用选择后起作用,而是使用 mywb.sheets(1).range("E:E")
【解决方案2】:

我之前遇到过这个问题,这是我的解决方案。

With Worksheets("Sheet1").Columns(5)
    .NumberFormat = "0"
    .Value = .Value
End With

【讨论】:

    【解决方案3】:

    这可用于查找工作表中的所有数值(甚至那些格式化为文本的数值)并将它们转换为单个数值(CSng 函数)。

    For Each r In Sheets("Sheet1").UsedRange.SpecialCells(xlCellTypeConstants)
        If IsNumeric(r) Then
           r.Value = CSng(r.Value)
           r.NumberFormat = "0.00"
        End If
    Next
    

    【讨论】:

    • 之前,我必须删除附加到数字的“B”(十亿)。只是这个解决方案有效,但我使用 CDec 因为 CSng 正在将 1.45 转换为 1.45000023,例如。
    【解决方案4】:

    这会将 Excel 工作簿列中的所有文本转换为数字。

    Sub ConvertTextToNumbers()
    Dim wBook As Workbook
    Dim LastRow As Long, LastCol As Long
    Dim Rangetemp As Range
    
    'Enter here the path of your workbook
    Set wBook = Workbooks.Open("yourWorkbook")
    LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastCol = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
    For c = 1 To LastCol
    Set Rangetemp = Cells(c).EntireColumn
    Rangetemp.TextToColumns DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
    Next c
    End Sub
    

    【讨论】:

    • TextTo Column 是最好的方法。在某些工作表上,.Value = .Value 方法存在问题(填充整列而不是填充的 200 行)
    【解决方案5】:

    对我来说有效的解决方案是:

    For Each xCell In Selection
    
      xCell.Value = CDec(xCell.Value)
    
    Next xCell 
    

    【讨论】:

      【解决方案6】:

      使用上面 aLearningLady 的回答,您可以通过查找包含数据的最后一行而不是仅选择整列来使您的选择范围动态化。

      下面的代码对我有用。

      Dim lastrow as Integer
      
      lastrow = Cells(Rows.Count, 2).End(xlUp).Row
      
      Range("C2:C" & lastrow).Select
      With Selection
          .NumberFormat = "General"
          .Value = .Value
      End With
      

      【讨论】:

        【解决方案7】:
        ''Convert text to Number with ZERO Digits and Number convert ZERO Digits  
        
        Sub ZERO_DIGIT()
         On Error Resume Next
         Dim rSelection As Range
         Set rSelection = rSelection
         rSelection.Select
         With Selection
          Selection.NumberFormat = "General"
          .Value = .Value
         End With
         rSelection.Select
          Selection.NumberFormat = "0"
         Set rSelection = Nothing
        End Sub
        
        ''Convert text to Number with TWO Digits and Number convert TWO Digits  
        
        Sub TWO_DIGIT()
         On Error Resume Next
         Dim rSelection As Range
         Set rSelection = rSelection
         rSelection.Select
         With Selection
          Selection.NumberFormat = "General"
          .Value = .Value
         End With
         rSelection.Select
          Selection.NumberFormat = "0.00"
         Set rSelection = Nothing
        End Sub
        
        ''Convert text to Number with SIX Digits and Number convert SIX Digits  
        
        
        Sub SIX_DIGIT()
         On Error Resume Next
         Dim rSelection As Range
         Set rSelection = rSelection
         rSelection.Select
         With Selection
          Selection.NumberFormat = "General"
          .Value = .Value
         End With
         rSelection.Select
          Selection.NumberFormat = "0.000000"
         Set rSelection = Nothing
        End Sub
        
        
        
        

        【讨论】:

          【解决方案8】:

          多次对我有用的解决方案是:

          Sub ConvertTextToNumber()
          
          With Range("A1:CX500") 'you can change the range 
          .NumberFormat = "General"
          .Value = .Value 
          End With
          
          End Sub
          

          【讨论】:

            【解决方案9】:

            对于大型数据集,需要更快的解决方案。

            利用“文本到列”功能提供了一种快速的解决方案。

            基于 F 列的示例,从 25 到 LastRow 的起始范围

            Sub ConvTxt2Nr()
            
            Dim SelectR As Range
            Dim sht As Worksheet
            Dim LastRow As Long
            
            Set sht = ThisWorkbook.Sheets("DumpDB")
            
            LastRow = sht.Cells(sht.Rows.Count, "F").End(xlUp).Row
            
            Set SelectR = ThisWorkbook.Sheets("DumpDB").Range("F25:F" & LastRow)
            
            SelectR.TextToColumns Destination:=Range("F25"), DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
                Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
                :=Array(1, 1), TrailingMinusNumbers:=True
            
            End Sub
            

            【讨论】:

            • 虽然文本到列是一种很好的方法,但您只能在一个列上使用它。如果您尝试使用 Text to Column 解决方案,那么您一次只能使用一列。
            【解决方案10】:

            我在使上述代码正常工作时遇到了问题。对我来说,乘以 1 就可以了:-)

                Cells(1, 1).Select
                Cells(1, 1) = ActiveCell * 1 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多