Excel VBA将存储为文本的数字转换为列中的数字

时间:2018-05-28 15:32:22

标签: excel vba excel-vba formatting

我的Excel工作表中有一列混合数据,其中数字格式为“文本”。 存储在我的列中的值的一些示例:

  • 36
  • R4
  • 56
  • AF PL RFD
  • 65
  • DHU
  • 14

我现在需要一个宏,将列中的所有数值转换为“Number”格式,以便我可以进一步处理数据。

我尝试了以下操作,在没有任何进一步解释的情况下抛出错误400:

Sheet1.Range("A2","A50000").Select
With Selection
    .NumberFormat = "General"
    .Value = .Value
End With

可能很难知道的是,我的列不包含多少行,因此我必须将范围设置得相当大。 谢谢你的帮助!

3 个答案:

答案 0 :(得分:1)

避免在同一语句中尝试选择工作表和范围(事实上,您可以完全避免选择:< / p>

Sub ytrewq()
    With Sheet1.Range("A2", "A50000")
        .NumberFormat = "General"
        .Value = .Value
    End With
End Sub

似乎工作得很好!

答案 1 :(得分:0)

我注意到许多人尝试用某种“数字格式”来回答这个问题,例如在Home Ribbon中。我认为关注点在于从源中提取数据,并且在单元格上悬停时在单元格中带有绿色小箭头并弹出“警告”字样。这无法通过数字格式解决,并且很可能已由寻求帮助的人尝试过。我就是其中之一。

With ActiveSheet.Select
    Range("A2:A10000").Select
        For Each xCell In Selection
        xCell.Value = CDec(xCell.Value)
    Next xCell
End With

Mabbutt,Dan。 “在Excel中将文本转换为数字。” ThoughtCo,2018年6月14日,thoughtco.com / convert-text-to-number-in-excel-3424223。

答案 2 :(得分:-1)

''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