我有数字列,无论出于何种原因,都被格式化为文本。这使我无法使用诸如小计函数之类的算术函数。转换这些"文本号码的最佳方法是什么?#34;真实的数字?
我试过这些片段无济于事:
Columns(5).NumberFormat = "0"
和
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
答案 0 :(得分:25)
使用以下功能(将[E:E]
更改为适合您需要的范围)以避免此问题(或更改为任何其他格式,例如&#34; mm / dd / yyyy&#34;):< / p>
[E:E].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
P.S。根据我的经验,这个VBA解决方案在大型数据集上的运行速度要快得多,并且与使用“警告框”相比,崩溃Excel的可能性更小。方法。
答案 1 :(得分:6)
这可以用来查找工作表中的所有数值(甚至是那些格式化为文本的数值)并将它们转换为单个(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
答案 2 :(得分:5)
我之前遇到过这个问题,这是我的解决方案。
With Worksheets("Sheet1").Columns(5)
.NumberFormat = "0"
.Value = .Value
End With
答案 3 :(得分:3)
这会将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
答案 4 :(得分:1)
对我来说,解决方案是:
For Each xCell In Selection
xCell.Value = CDec(xCell.Value)
Next xCell
答案 5 :(得分:0)
对于大型数据集,需要更快的解决方案。
使用“文本到列”功能提供了一种快速的解决方案。
基于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
答案 6 :(得分:0)
使用上面的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 :(得分:0)
''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