2012-01-13 96 views
1

这似乎是这样一个简单的要求,我觉得我缺少明显的东西。在Excel中删除前后空格和逗号

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

注意:我不想替换所有这些字符,因为它们合法地出现在单元格文本中 - 只是在单元格文本(即值)的开始或结尾时,我想将它们关闭。

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

那么,有没有内置函数,还是我需要写一个?我感到的字符串过滤功能的数目在变质PHP ;-)

+0

我想补充,这些细胞我清洗有逗号,空格,你能想到的换行符每个组合和顺序的,所以我不能依次剥去每一个。 – Jason 2012-01-13 01:04:31

+0

你在Excel中做这个或者是一个创建这个文件的vb/c#程序吗? – em3ricasforsale 2012-01-13 01:17:59

+0

数据全部以Excel格式提供。这个想法是在Excel中生成工作表,我可以导出为CSV格式导入到CRM中。有这样小的清理,但大部分工作涉及州/县/称呼表的验证和查找等。 – Jason 2012-01-13 01:23:09

回答

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 
+0

最后,我使用了我粘贴到问题中的代码,因为它更简单,但会保留您的版本以方便将来使用,因为它看起来非常方便。然而,我不想在现场修复数据,因为在电子表格世界中,我喜欢从源代码到最终数据的全部工作,因为它使得发现不可避免的错误,不正确的假设和丢失的数据变得更容易。 – Jason 2012-01-13 02:10:04

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 
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上使用过它,注意在它坠毁之前可以添加超过10个单元!)。这个单元的功能是能够做我需要的修整:

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

这个工程很漂亮。

(注:这个答案从问题文本,它真的不应该被移动。)