Convert Number stored as Text to Number in column
我的 Excel 工作表中有一列混合数据,其中的数字格式为 "Text"。
一些例子:
- 36
- R4
- 56
- AF PL RFD
- 65
- DHU
- 14
我需要将列中的所有数值转换为"数字"格式,以便进一步处理数据。
以下抛出错误 400 没有任何进一步的解释:
1 2 3 4 5 | Sheet1.Range("A2","A50000").Select With Selection .NumberFormat ="General" .Value = .Value End With |
我不知道我的列将包含多少行的值,所以我必须使范围相当大。
我注意到许多人试图用某种"数字格式"来回答这个问题,例如在主页功能区中找到的。我认为问题在于从源中提取数据并在单元格中显示绿色小箭头,并在将鼠标悬停在单元格上时弹出"警告"。这无法通过数字格式解决,并且很可能已由寻求帮助的人尝试过。我是其中之一。
1 2 3 4 5 6 | With ActiveSheet.Select Range("A2:A10000").Select For Each xCell In Selection xCell.Value = CDec(xCell.Value) Next xCell End With |
马布特,丹。"在 Excel 中将文本转换为数字。" ThoughtCo,2018 年 6 月 14 日,thoughtco.com/convert-text-to-number-in-excel-3424223。
避免在同一个语句中同时选择工作表和范围(事实上,您可以完全避免选择):
1 2 3 4 5 6 | Sub ytrewq() With Sheet1.Range("A2","A50000") .NumberFormat ="General" .Value = .Value End With End Sub |
似乎工作得很好!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | ''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 |