VBA:将文本转换为数字

我有列的数字,无论出于何种原因,被格式化为文本。 这可以防止我使用诸如小计function之类的算术函数。 将这些“文本数字”转换为真实数字的最佳方法是什么?

以下是具体问题的截图: 错误

我已经试过这些片段无济于事:

Columns(5).NumberFormat = "0" 

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

使用下面的function(将[E:E]更改为适合您需要的范围)来解决此问题:

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

我以前有这个问题,这是我的解决scheme。

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

这将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 

这可以用来查找表格中的所有数值(甚至那些格式为文本),并将其转换为单个(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