修剪Excel单元格中的空白

我如何摆脱excel领先的空白?

我有相当多的行这个问题。

假设你想摆脱A列中的空格

  1. 转到一个空的列(让我们说B)
  2. 在B1中input=TRIM(A1)
  3. 将此公式向下填入所有B行。
  4. 然后将B列复制到剪贴板,并使用“粘贴内容”将B列的值(不是公式)复制回A列。

在你的空白删除请求请注意:

  • TRIM只删除字符32,即标准空间。
  • CLEAN将删除非打印空白,例如回车符(字符13)和换行符(字符10)
  • CLEAN不处理不间断的空格(字符160),这是处理来自Web的数据的常见问题,因此您需要一个SUBSTITUTE函数

你可以用整个单元格的公式来做到这一点,也可以用vba来更加轻松地(和定位领先的空格)

  1. 使用公式,您可以结合使用这三个公式来清理整个单元格,例如=TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))) 。 在这里看到罗恩·德·布朗斯的文字

  2. 下面的VBA将有效地使用数组和正则expression式就地replace您的数据,而不需要任何工作列和复制和粘贴。 下面的代码包含使用说明。 与公式选项不同,此代码仅在string的前导部分上起作用

      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 

你可以使用内置的TRIMfunction吗? 它从传入文本的开头和结尾删除空格:

 =TRIM(A1) 

这个公式会给你从单元格A1没有前导或尾随白色空间的文本。

要使用公式,可以在原始列旁边插入一列。 然后将公式input到第一个(顶部)空单元格中,将原始列的第一个(顶部)单元格的单元格坐标replace为“A1”,然后按Enter键。 然后,您可以抓住单元格的右下angular(光标将变为“+”符号),然后将框拖到新列中的最后一个单元格,以重复列中所有行的公式。