VBA将文本转换为除公式和非数字文本之外的数字

我有一个Range("B6:T10000")

范围内的数据是blanks#'snumbers (formatted as texts)texts和最重要的formulas

有人可以请帮助一个VBAmacros来:

  • find任何看起来像数字,并将其转换为数字
  • 忽略其余的
  • 不要将公式转换为值

非常感谢你

试试这个:

 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 

编辑#1

此版本忽略错误:

 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 

你可以做到这一点,没有代码,或避免循环更快的代码

手册

  1. 复制一个空白单元格
  2. select您的范围B6:T100001
  3. F5 。 然后Goto ... Special
  4. 选中Constants ,然后selectText
  5. Paste Special Multiply然后selectAdd

这只将带有数字的单元格转换成数字,并且只留下实际的文本或公式

 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 

这是我的版本:

 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 

我testing过了,工作正常。
希望这可以帮助。

 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