[英]VBA to convert texts to numbers except formula and non-numeric texts
I have a Range("B6:T10000")
我有一个Range("B6:T10000")
Data in the range are a mixture of blanks
, #'s
, numbers (formatted as texts)
, texts
and most importantly formulas
. 范围内的数据是blanks
, #'s
, numbers (formatted as texts)
, texts
和最重要的formulas
。
Can someone please help with a VBA macro to: 有人可以帮助使用VBA宏来:
Thank you very much 非常感谢你
You can do this without code, or with quicker code avoiding loops 您可以在没有代码的情况下执行此操作,或使用更快的代码避免循环
Manual 手册
B6:T100001
选择您的范围B6:T100001
F5
. 按F5
。 Then Goto ... Special
然后Goto ... Special
Constants
and then Text
选中Constants
,然后选择Text
Paste Special Multiply
and check Add
Paste Special Multiply
并选中Add
This converts text only cells with numbers into numbers, and leaves actual text or formulae alone 这会将带有数字的文本单元格转换为数字,并仅保留实际文本或公式
Code 码
Sub Update()
Dim rng1 As Range
On Error Resume Next
Set rng1 = Range("B6:T10000").SpecialCells(xlCellTypeConstants, 2)
On Error Resume Next
If rng1 Is Nothing Then Exit Sub
'presumes last cell in sheet is blank
Cells(Rows.Count, Columns.Count).Copy
rng1.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
End Sub
here's my version: 这是我的版本:
Sub Test()
Dim rng as Range, cel as Range
Set rng = Thisworkbook.Sheets("Sheet1").Range("B6:T10000")
For Each cel In rng
If Not IsError(cel.Value) Then _
If Len(cel.Value) <> 0 And cel.HasFormula = False And _
IsNumeric(cel.Value) Then cel.Value = Val(cel.Value)
Next cel
End Sub
I've tested it, and works fine. 我测试过它,并且工作正常。
Hope this helps. 希望这可以帮助。
Give this a try: 尝试一下:
Sub Converter()
Dim rBig As Range, r As Range, v As Variant
Set rBig = Range("B6:T10000")
For Each r In rBig
v = r.Value
If v <> "" And r.HasFormula = False Then
If IsNumeric(v) Then
r.Clear
r.Value = v
End If
End If
Next r
End Sub
EDIT#1 : 编辑#1 :
This version ignores errors: 此版本忽略错误:
Sub Converter()
Dim rBig As Range, r As Range, v As Variant
Set rBig = Range("B6:T10000")
For Each r In rBig
v = r.Value
If Not IsError(v) Then
If v <> "" And r.HasFormula = False Then
If IsNumeric(v) Then
r.Clear
r.Value = v
End If
End If
End If
Next r
End Sub
ActiveSheet.Range("b5:b6004,h5:h6004").Select
For Each xCell In Selection
If IsNumeric(xCell) = False Then
xCell.Value = Val(xCell.Value)
Else
End If
Next xCell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.