Trim Cells using VBA in Excel

Posted by Greg Reynolds on Stack Overflow See other posts from Stack Overflow or by Greg Reynolds
Published on 2010-06-03T09:49:41Z Indexed on 2010/06/03 12:44 UTC
Read the original article Hit count: 203

Filed under:
|
|

I have what seems like a simple problem with some data in Excel. I have a lot of data with leading spaces pasted from a web table, and I would like to get rid of the initial space. I cribbed the following code (I am completely new to VBA), but it doesn't seem to work. When I step through it in the debugger it looks like an infinite loop. Any help would be appreciated!

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

© Stack Overflow or respective owner

Related posts about excel

Related posts about vba