Regex VBA编辑excel数据列

Regex VBA编辑excel数据列,regex,excel,vba,Regex,Excel,Vba,我需要编辑一列数据,其中每个单元格都必须编辑以删除所有非数字字符。我需要的唯一数据是实际数字和小数点(如果最初有)。我发现了一段代码,它删除了除“%”字符以外的所有内容。如果有人能看看下面的代码,让我知道如何修改它,我将不胜感激。我正在编辑的数据类型示例如下(完整单元格内容用引号括起来)。“3”“2.5%”“17 nks”“3.00%”“4 VNS” 这是我使用的代码 Sub RemoveAlphas() '' Remove alpha characters from a string. Dim

我需要编辑一列数据,其中每个单元格都必须编辑以删除所有非数字字符。我需要的唯一数据是实际数字和小数点(如果最初有)。我发现了一段代码,它删除了除“%”字符以外的所有内容。如果有人能看看下面的代码,让我知道如何修改它,我将不胜感激。我正在编辑的数据类型示例如下(完整单元格内容用引号括起来)。“3”“2.5%”“17 nks”“3.00%”“4 VNS”

这是我使用的代码

Sub RemoveAlphas()
'' Remove alpha characters from a string.
Dim intI As Integer
Dim rngR As Range, rngRR As Range
Dim strNotNum As String, strTemp As String

Set rngRR = Selection.SpecialCells(xlCellTypeConstants, _
xlTextValues)

For Each rngR In rngRR
    strTemp = ""
    For intI = 1 To Len(rngR.Value)
        If Mid(rngR.Value, intI, 1) Like "[0-9,.]" Then
            strNotNum = Mid(rngR.Value, intI, 1)
        Else: strNotNum = ""
        End If
        strTemp = strTemp & strNotNum
    Next intI
    rngR.Value = strTemp
Next rngR

End Sub

谢谢。

这可以使用如下所示的正则表达式来完成-已使用您的精确样本数据进行了测试,适用于我:

Sub RemoveAlphas()
'' Remove alpha characters from a string.
Dim intI As Integer
Dim rngR As Range, rngRR As Range
Dim strNotNum As String, strTemp As String
Dim RegEx As Object

   Set rngRR = Selection.SpecialCells(xlCellTypeConstants, _
   xlTextValues)
   Set RegEx = CreateObject("VBScript.RegExp")
   RegEx.Global = True
   RegEx.Pattern = "[^\d.]+"
   For Each rngR In rngRR
      rngR.Value = RegEx.Replace(rngR.Value, "")
   Next rngR
End Sub

如果您得到的是除百分号之外所需的结果,则可以在代码的这一部分插入替换函数:

    Next intI
    strTemp = Replace(strTemp, "%", "")   'Remove the % sign and replace with nothing.
    rngR.Value = strTemp
Next rngR

尝试下面的RexExp,它基于我的源代码构建,并使用变量数组提高速度

RegExp
模式是
[^\d\.]+

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 = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
    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.Pattern=“[^\d-557;]+“rngR.Value=regex.Replace(rngR.Value)”)替换for循环中的regex表达式-”-“rngR.Value=regex.Replace(rngR.Value)”
谢谢标记。很抱歉回答延迟-我不在我的机器旁。你是说除了不能去掉“%”之外,你的手机还能正常工作吗?你可能会发现手机已正确地缩小为一个数字,但手机的格式是百分比。