Trim Cells using VBA in Excel

后端 未结 8 1158
你的背包
你的背包 2020-12-11 03:02

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

相关标签:
8条回答
  • 2020-12-11 03:50

    Works fine for me with one change - fourth line should be:

    cell.Value = Trim(cell.Value)
    

    Edit: If it appears to be stuck in a loop, I'd add

    Debug.Print cell.Address
    

    inside your For ... Next loop to get a bit more info on what's happening.

    I also suspect that Trim only strips spaces - are you sure you haven't got some other kind of non-display character in there?

    0 讨论(0)
  • 2020-12-11 03:50

    Worked for me perfectly as this:

    Trims all selected cells. Beware of selecting full columns/rows :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
    
    0 讨论(0)
提交回复
热议问题