从Excel单元格中修剪前导空格

Sle*_*lee 1 excel vba excel-vba excel-formula

如何摆脱的领先空白?

这个问题我有很多行.

bre*_*tdj 10

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

  • TRIM 仅删除字符32,即标准空间.
  • CLEAN 将删除非打印空格,例如回车(字符13)和换行符(字符10)
  • CLEAN不处理非破坏空间(字符160),这是处理来自网络数据的常见问题,为此您需要一个SUBSTITUTE功能

您可以使用整个单元格中的公式执行此操作,也可以使用更轻松地执行此操作(并使用前导空格)

  1. 使用Formula,您可以结合使用这三个公式来清理整个单元格 =TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))).见罗恩德熊的书面记录在这里

  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
    
    Run Code Online (Sandbox Code Playgroud)


Doc*_*own 5

让我们假设您想要删除A列中的空格.

  1. 转到空列(比方说B)
  2. 进入=TRIM(A1)B1.
  3. 将此公式向下填充到B的所有行中.
  4. 然后将列B复制到剪贴板并使用"粘贴内容"将列B的值(而不是公式)复制回到列A.