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

yin*_*nka 2 excel vba excel-vba excel-2010

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

在范围内的数据是的混合物blanks,#'s,numbers (formatted as texts),texts和最重要的formulas.

有人可以帮助使用VBA宏来:

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

非常感谢你

bre*_*tdj 5

您可以在没有代码的情况下执行此操作,或使用更快的代码避免循环

手册

  1. 复制一个空白单元格
  2. 选择您的范围 B6:T100001
  3. F5.然后Goto ... Special
  4. 检查Constants然后Text
  5. Paste Special Multiply 并检查 Add

这会将带有数字的文本单元格转换为数字,并仅保留实际文本或公式

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
Run Code Online (Sandbox Code Playgroud)