2012-01-13 98 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,]+$", "") 

這個工程很漂亮。

(注:這個答案從問題文本,它真的不應該被移動。)