使用以下函数(将[E:E]
更改为您的需求的适当范围),以避免此问题(或更改为任何其他格式,例如“mm/dd/yyyy”):
使用下面的函数(将[E:E]
替换为您需要的适当范围),即可解决此问题(或更改为任何其他格式,如“mm/dd/yyyy”):
[E:E].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
顺便说一下,根据我的经验,这个VBA解决方案在处理大数据集时工作速度明显更快,并且比使用“警告框”方法更不容易导致Excel崩溃。
TextToColumn
方法似乎更可靠。 - iDevlopvar = ActiveSheet.UsedRange.Rows.Count
Range("E1:E" & var).Select
在以下代码之前:Selection.Value = .Value
- Jason R. Escamilla这可以用来查找表格中所有数值(即使是以文本格式呈现的)并将它们转换为单精度浮点数(使用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
CDec
,因为CSng
会将1.45转换为1.45000023,例如。 - xm1我之前也遇到过这个问题,以下是我的解决方案。
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
.Value = .Value
方法出现了问题(整个列被填充,而不是200行)。 - iDevlop
Sub ConvertTextToNumber()
With Range("A1:CX500") 'you can change the range
.NumberFormat = "General"
.Value = .Value
End With
End Sub
''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
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
对我来说适用的解决方案是:
For Each xCell In Selection
xCell.Value = CDec(xCell.Value)
Next xCell
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
从记录的宏中得到以下代码;对于新应用程序,您只需要更新选择和范围:
Sub num()
Columns("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
End Sub