gpt4 book ai didi

excel - 修剪 Excel 单元格中的前导空格

转载 作者:行者123 更新时间:2023-12-04 21:07:56 26 4
gpt4 key购买 nike

如何去掉 中的前导空格?

我有很多行有这个问题。

最佳答案

在您的空格删除请求中,请注意:

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

  • 您可以使用整个单元格上的公式来执行此操作,或者使用 更轻松地(并针对前导空格)执行此操作。
  • 使用公式,您可以结合使用这三个公式来清理整个单元格,就像这样=TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))) .请参阅 Ron de Bruins 的文章 here
  • 下面的 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
  • 关于excel - 修剪 Excel 单元格中的前导空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811736/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com