2011-10-18 100 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