简体   繁体   English

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

[英]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#'snumbers (formatted as texts)texts和最重要的formulas

Can someone please help with a VBA macro to: 有人可以帮助使用VBA宏来:

  • Find anything that looks like number and convert it to number 找到任何看起来像数字的东西并将其转换为数字
  • Ignore the rest 忽略其余的
  • Don't convert formulas to values 不要将公式转换为值

Thank you very much 非常感谢你

You can do this without code, or with quicker code avoiding loops 您可以在没有代码的情况下执行此操作,或使用更快的代码避免循环

Manual 手册

  1. Copy a blank cell 复制一个空白单元格
  2. Select your range B6:T100001 选择您的范围B6:T100001
  3. Press F5 . F5 Then Goto ... Special 然后Goto ... Special
  4. check Constants and then Text 选中Constants ,然后选择Text
  5. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM