在Excel中删除前导/尾随空格和逗号

时间:2012-01-13 01:01:01

标签: excel excel-vba trim vba

这似乎是一个如此简单的要求,我觉得我错过了一些明显的东西。

我有一个带有“脏”文本数据的Excel电子表格,其中包含文本和不需要的前导和尾随,空格,逗号和换行符。我想TRIM引用所有这些字符的这些单元格。

注意:我不想替换所有这些字符,因为它们合法地出现在单元格文本中 - 它恰好在单元格文本的开头或结尾处(即值)我想要将它们剪掉。

文本数据由人员和学校的名称组成,用于清理和导入CRM。

那么,是否内置了一个函数,还是需要编写一个函数?我感到被PHP中的字符串过滤函数的数量所破坏; - )

4 个答案:

答案 0 :(得分:2)

这非常适合正则表达式

adapted from this article下方的代码使用此正则表达式 "[,\s]*(.+?)[,\s]*$"
删除任何前导和/或尾随空格/逗号,同时在文本正文中保留任何此类字符

它将原位替换现有数据

Sub RemoveDirt()
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 leading zeros", "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.MultiLine = True
objReg.Pattern = "[,\s]*(.+?)[,\s]*$"

'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), "$1")
            Next lngCol
        Next lngRow
        'Dump the updated array sans dirt 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, "$1")
    End If
Next rngArea

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

Set objReg = Nothing
End Sub

答案 1 :(得分:1)

我找到了这段代码,我将其作为模块粘贴到我的电子表格中:

Option Explicit

Function ReReplace(ReplaceIn, _
    ReplaceWhat As String, ReplaceWith As String, Optional IgnoreCase As Boolean = False)

    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    RE.IgnoreCase = IgnoreCase
    RE.Pattern = ReplaceWhat
    RE.Global = True
    ReReplace = RE.Replace(ReplaceIn, ReplaceWith)
End Function

这提供了一个支持RE的替换功能(为什么Excel不会这样做?它自1987年以来一直存在 - 我在Atari ST上使用它,请注意你可以在它之前添加十个以上的单元格崩溃!)。这个细胞功能能够进行我需要的修剪:

=ReReplace('source worksheet'!cell_reference, "^[\s,]+|[\s,]+$", "")

这很有效。

(注意:这个答案来自问题文本,它实际上不应该出现。)

答案 2 :(得分:0)

用于删除逗号和尾随空格的递归函数。纯VBA ..

Function removetrailcomma(txt As String) As String
    If Right(txt, 1) = " " Or Right(txt, 1) = "," Then
        removetrailcomma = removetrailcomma(Left(txt, Len(txt) - 1))
    Else
        removetrailcomma = txt
    End If
End Function

答案 3 :(得分:0)

我尝试使用两个步骤

  1. 删除空格
  2. 删除逗号
  3. 用于删除前导和尾随空格

    使用直接功能TRIM(A1)

    用于删除前导和尾随逗号

    =MID(A1,IF(FIND(",",A1)=1,2,1),IF(RIGHT(A1)=",",LEN(A1)-2,LEN(A1)))
    

    =SUBSTITUTE(TRIM(SUBSTITUTE(A1,","," "))," ",",")