2011-10-18 102 views

回答

5

讓我們假設你想在A列

  1. 轉到一個空列擺脫空格的(可以說B)
  2. 輸入=TRIM(A1)爲B1。
  3. 向下填充此公式成B.
  4. 的所有行然後塔B複製到剪貼板,並使用「糊狀內容物」,以列B的值(不是式)複製回塔A.
+1

在Office 2011中,您必須執行「選擇性粘貼」,並確保選擇粘貼值。方法略有不同,結果相同。 – peelman

+0

@peelman:Office 2011? –

+0

是... Office 2011 ... http://www.microsoft.com/mac – peelman

2

你可以使用內置的TRIM功能嗎?它消除了空白,從傳遞給它的文本的開頭和結尾:

=TRIM(A1) 

這個公式會給你從A1單元格文本沒有前導或尾隨whitepace。

要使用該公式,您需要在原始列旁邊插入一列。然後將公式輸入到第一個(頂部)空單元格中,將原始列的第一個(頂部)單元格的單元格座標替換爲「A1」,然後按enter鍵。然後,您可以抓住單元格的右下角(光標將更改爲「+」符號),然後將該框向下拖動到新列中的最後一個單元格,以重複列中所有行的公式。

+0

但我怎麼究竟做 - 選擇列行,然後呢? – Slee

+0

@Slee:我更新了我的答案。如果您對公式不熟悉,Google或Excel的幫助功能可幫助您入門。 –

10

在您的空格刪除請求請注意:

  • TRIM只刪除字符32,即一個標準的空間。
  • CLEAN將除去非印刷的空白如回車(字符13)和換行(字符10)
  • CLEAN不處理非中斷空格(字符160),與涉及從數據的一個常見問題網絡,爲此,你需要一個SUBSTITUTE功能

你可以做到這一點無論其在整個單元格公式,或更怕疼(和指定的前導空格)與

  1. 使用公式,您可以結合使用這三個公式來清理整個單元,例如 =TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," ")))。請參閱Ron de Bruins的書寫here

  2. 下面的VBA將使用數組和正則表達式就地替換您的數據,而無需任何工作列和複製粘貼。下面的代碼包含使用說明。此代碼僅適用於字符串的開頭部分,不像公式選項

    Sub KillLeadingSpaces() 
    
        'Press Alt + F11 to open the Visual Basic Editor (VBE) 
        'From the Menu, choose Insert-Module. 
        'Paste the code into the right-hand code window. 
        'Press Alt + F11 to close the VBE 
        'In Xl2003 Goto Tools ....Macro .... Macros and double-click KillLeadingSpaces 
        'In Xl2007/10 Goto Developer .. Macros and double-click KillLeadingSpaces 
    
        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.Pattern = "^[\s|\xA0]+" 
    
        '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 whitepace 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