【问题标题】:Trim Cells using VBA in Excel在 Excel 中使用 VBA 修剪单元格
【发布时间】:2010-06-03 09:49:41
【问题描述】:

Excel 中的一些数据似乎是一个简单的问题。我有很多从 web 表中粘贴的带有前导空格的数据,我想摆脱初始空间。我抄写了以下代码(我对 VBA 完全陌生),但它似乎不起作用。当我在调试器中单步执行它时,它看起来像一个无限循环。任何帮助将不胜感激!

Sub DoTrim()
  For Each cell In Selection.Cells
    If cell.HasFormula = False Then
      cell = Trim(cell)
    End If
  Next
End Sub

编辑: 看起来 TRIM 函数遇到了“空格”字符的问题。这段代码:

Sub DoTrim()
Dim cell As Range, areaToTrim As Range
Set areaToTrim = Selection.Cells
For Each cell In areaToTrim
    cell.Value = "MUPPET"
Next cell
End Sub

更改了单元格的值,所以我猜它是一个非空格空格!关于如何摆脱这些的任何想法?

【问题讨论】:

  • chr(255) 在 Excel 中显示为空格。这通常是导致问题的原因。

标签: vba excel


【解决方案1】:

这对我很有效。它使用一个数组,因此您不会遍历每个单元格。在大型工作表部分上运行得更快。

Sub Trim_Cells_Array_Method()

Dim arrData() As Variant
Dim arrReturnData() As Variant
Dim rng As Excel.Range
Dim lRows As Long
Dim lCols As Long
Dim i As Long, j As Long

  lRows = Selection.Rows.count
  lCols = Selection.Columns.count

  ReDim arrData(1 To lRows, 1 To lCols)
  ReDim arrReturnData(1 To lRows, 1 To lCols)

  Set rng = Selection
  arrData = rng.value

  For j = 1 To lCols
    For i = 1 To lRows
      arrReturnData(i, j) = Trim(arrData(i, j))
    Next i
  Next j

  rng.value = arrReturnData

  Set rng = Nothing
End Sub

【讨论】:

    【解决方案2】:

    我会尝试在没有 VBA 的情况下解决这个问题。只需选择此 空格 并在您试图摆脱那些 空格 的工作表上使用替换(更改为空)。

    如果你真的想使用 VBA,我相信你可以选择第一个字符

    strSpace = left(range("A1").Value,1)
    

    并以同样的方式在 VBA 中使用替换功能

    Range("A1").Value = Replace(Range("A1").Value, strSpace, "")
    

    for each cell in selection.cells
     cell.value = replace(cell.value, strSpace, "")
    next
    

    【讨论】:

      【解决方案3】:

      如果字符串前面有非打印字符,试试这个

      
      Option Explicit
      Sub DoTrim()
          Dim cell As Range
          Dim str As String
          Dim nAscii As Integer
          For Each cell In Selection.Cells
              If cell.HasFormula = False Then
                  str = Trim(CStr(cell))
                  If Len(str) > 0 Then
                      nAscii = Asc(Left(str, 1))
                      If nAscii &lt 33 Or nAscii = 160 Then
                          If Len(str) &gt 1 Then
                              str = Right(str, Len(str) - 1)
                          Else
                              strl = ""
                          End If
                      End If
                  End If
                  cell=str
              End If
          Next
      End Sub
      

      【讨论】:

      • 字符串前面还有一个空格符
      【解决方案4】:

      对我来说只需进行一项更改即可正常工作 - 第四行应该是:

      cell.Value = Trim(cell.Value)
      

      编辑:如果它似乎卡在一个循环中,我会添加

      Debug.Print cell.Address
      

      在您的 For ... Next 循环中获取有关正在发生的事情的更多信息。

      我还怀疑Trim 只去除空格 - 你确定你没有其他类型的非显示字符吗?

      【讨论】:

        【解决方案5】:

        完美地为我工作:

        修剪所有选定的单元格。小心选择完整的列/行:P.

        Sub TrimSelected()    
        Dim rng As Range, cell As Range    
        Set rng = Selection   
        
        For Each cell In rng    
        cell = Trim(cell)   
        
        Next cell    
        
        End Sub
        

        【讨论】:

          【解决方案6】:

          无限循环是代码中较高位置的On Error Resume Next 语句的结果。现在摆脱它。如果您的代码仅在 On Error Resume Next 有效的情况下运行,则需要立即彻底检修。

          当您使用它时,在您的模块顶部放置一个Option Explicit。它强制您声明所有变量,以防止因变量名输入错误而导致的恼人错误。 (您可以修改 VBA 编辑器首选项以在下次自动设置该选项,强烈建议这样做。)

          您的代码的直接问题是单元格是一个对象,并且无法修剪对象。你想修剪单元格的文本,所以你必须这样做:

          cell.Text = Trim(cell.Text)
          

          【讨论】:

          • 这是我个人工作簿中唯一的宏。而且它仍然不适用于 cell.Value 版本。
          • @Greg Reynolds:我的错,这应该是.Text,我已经更正了我的答案。
          • 使用 .text 通常不是一个好主意,因为它会为您提供 Excel 格式化的值(如果用户更改了列宽,则可能是 ###)而不是实际值。此外,Range 对象的默认属性是 .value,因此如果 Cell 是 range 对象,则 Trim(Cell) 可以正常工作。
          【解决方案7】:

          这应该可以完成您想做的事情。我只是在我的一张纸上测试了它;如果这不起作用,请告诉我。如果您只有前导空格,则为 LTrim;可以使用 Trim 函数以及它处理前导和尾随空格。将我拥有的区域中的单元格范围替换为“A1:C50”,并确保将“Sheet1”更改为您正在处理的工作表的名称。

          Dim cell As Range, areaToTrim As Range
          Set areaToTrim = Sheet1.Range("A1:C50")
          For Each cell In areaToTrim
              cell.Value = LTrim(cell.Value)
          Next cell
          

          【讨论】:

            【解决方案8】:

            唯一对我有用的是这个功能:

            Sub DoTrim()
            Dim cell As Range
            Dim str As String
            For Each cell In Selection.Cells
                If cell.HasFormula = False Then
                    str = Left(cell.Value, 1) 'space
                    While str = " " Or str = Chr(160)
                        cell.Value = Right(cell.Value, Len(cell.Value) - 1)
                        str = Left(cell.Value, 1) 'space
                    Wend
                    str = Right(cell.Value, 1) 'space
                    While str = " " Or str = Chr(160)
                        cell.Value = Left(cell.Value, Len(cell.Value) - 1)
                        str = Right(cell.Value, 1) 'space
                    Wend
                End If
            Next cell
            End Sub
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-01-27
              • 2013-03-13
              • 2012-09-09
              • 2013-07-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多