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

这似乎是这样一个简单的要求,我觉得我失去了一些明显的东西。

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

注意:我不想replace所有这些字符,因为它们合法地出现在单元格文本中 – 就是在单元格文本(即值)的开始或结尾处,我想将它们修剪掉。

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

那么,有没有内置的函数,还是我需要写一个? 我觉得在PHP中的string过滤函数的数量被宠坏了;-)

这非常适合于正则expression式

下面的代码改编自这篇文章使用这个正则expression式
"[,\s]*(.+?)[,\s]*$"
删除任何前导和/或尾随的空白/逗号,同时保持文本体内的任何这样的字符完好无损

它将原地replace您现有的数据

 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 

recursion函数删除逗号和尾随空格。 纯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 

我find了这个代码,我把它作为一个模块粘贴到我的电子表格中:

 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的replace函数(Excel为什么不自己做这件事呢?它自从1987年以来一直存在 – 我在Atari ST上使用过它,而不是在它崩溃之前可以添加更多的十个单元格)。 这个单元格function能够做我需要的修整:

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

这工作很好。

(注意:这个答案是从问题文本中移出来的,它本来是不应该的。)