aLe*_*ady 15 excel formatting vba excel-vba
我有数字列,无论出于何种原因,都被格式化为文本.这使我无法使用诸如小计函数之类的算术函数.将这些"文本数字"转换为真实数字的最佳方法是什么?
我试过这些片段无济于事:
Columns(5).NumberFormat = "0"
Run Code Online (Sandbox Code Playgroud)
和
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Run Code Online (Sandbox Code Playgroud)
aLe*_*ady 25
使用以下功能([E:E]
根据需要更改为适当的范围)以避免此问题(或更改为任何其他格式,例如"mm/dd/yyyy"):
[E:E].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
Run Code Online (Sandbox Code Playgroud)
PS根据我的经验,这个VBA解决方案在大型数据集上的运行速度要快得多,并且与使用"警告框"方法相比,崩溃Excel的可能性更小.
小智 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
Run Code Online (Sandbox Code Playgroud)
小智 5
我之前有这个问题,这是我的解决方案。
With Worksheets("Sheet1").Columns(5)
.NumberFormat = "0"
.Value = .Value
End With
Run Code Online (Sandbox Code Playgroud)