如何使用VBA代码从整个Excel中删除多余的空格?

时间:2017-07-12 12:23:08

标签: excel vba excel-vba

Set r = Worksheets("Sheet1").Columns("A").Find(What:="  ")
     If r Is Nothing Then
            MsgBox "done"

\        万一 结束子

1 个答案:

答案 0 :(得分:1)

以下代码删除了col A中文本中的所有空格。希望这是您正在寻找的内容。必要时修改代码。 我已完成更改,以便它适用于整个工作表。如果它解决了您的要求,请单击“接受”来接受答案

   Sub teste()

    Dim UsedRng As Range
    Dim FirstRow As Long, LastRow As Long, FirstCol As Long, LastCol As Long

    Set UsedRng = ActiveSheet.UsedRange

    FRow = UsedRng(1).Row
    FCol = UsedRng(1).Column
    lRow = UsedRng(UsedRng.Cells.Count).Row
    Lcol = UsedRng(UsedRng.Cells.Count).Column

For X = FRow To lRow

For Y = FCol To Lcol
        temp = Cells(X, Y).Value
        tempC = (Trim(temp))
        Cells(X, Y).Value = tempC
        Next Y
Next X
End Sub