regex - 从单元格区域中删除非数字字符

标签 regex excel excel-formula vba

  1. 我有一个 1500 行的 Excel 文件。
  2. 在第一列中,字符与数字混合在一起(每行都不同)。
  3. 我想只保留数字并删除字母字符。

例如 A1我有

f90f5j49,35.48

删除字母字符后,它应该是

905493548

最好的方法是什么?

我确实找到了这个youtube solution

谢谢!

最佳答案

你的方法是正确的方法。

实现这一点的最快方法是使用

  • 一个
  • 以及 VBA 中的变量数组。

使用基于我的文章 Using Variant Arrays in Excel VBA for Large Scale Data Manipulation 的代码

Sub KillNonNumbers()
    Dim rng1 As Range
    Dim rngArea As Range
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngCalc As Long
    Dim objReg As Object
    Dim X()


    On Error Resume Next
    Set rng1 = Application.InputBox("Select range for the replacement of non-number", "User select", Selection.Address, , , , , 8)
    If rng1 Is Nothing Then Exit Sub
    On Error GoTo 0

    'See Patrick Matthews excellent article on using Regular Expressions with VBA
    Set objReg = CreateObject("vbscript.regexp")
    objReg.Pattern = "[^\d]+"
    objReg.Global = True

   'Speed up the code by turning off screenupdating and setting calculation to manual
   'Disable any code events that may occur when writing to cells
    With Application
        lngCalc = .Calculation
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With

    'Test each area in the user selected range

    'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
    For Each rngArea In rng1.Areas
        'The most common outcome is used for the True outcome to optimise code speed
        If rngArea.Cells.Count > 1 Then
           'If there is more than once cell then set the variant array to the dimensions of the range area
           'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks
            X = rngArea.Value2
            For lngRow = 1 To rngArea.Rows.Count
                For lngCol = 1 To rngArea.Columns.Count
                    'replace the leading zeroes
                    X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), vbNullString)
                Next lngCol
            Next lngRow
            'Dump the updated array sans leading zeroes back over the initial range
            rngArea.Value2 = X
        Else
            'caters for a single cell range area. No variant array required
            rngArea.Value = objReg.Replace(rngArea.Value, vbNullString)
        End If
    Next rngArea

    'cleanup the Application settings
    With Application
        .ScreenUpdating = True
        .Calculation = lngCalc
        .EnableEvents = True
    End With

    Set objReg = Nothing
End Sub

关于regex - 从单元格区域中删除非数字字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10789948/

相关文章:

vba - 如何最好地构建 Excel VBA 代码以实现维护、清晰度和错误处理

excel - 从字符串中删除前导和尾随空格,同时保留其间的空格

Excel - 在没有帮助列的情况下计算其值满足复杂条件的行

php - 字符串后如果没有空格,正则表达式 OR 语句将无法工作

MYSQL 替换空格分隔字符串的一部分

regex - Perl 正则表达式替换计数

excel - 如何将月份编号(即 "12")转换为 Excel 中的月份名称?

javascript - 使用正则表达式在等号后显示文本?

excel - 如何使用 WSH 阅读电子表格?

excel - 看似非常简单的 Excel 无响应问题