How to remove spaces in between text?

后端 未结 10 748
日久生厌
日久生厌 2021-01-13 00:00

Why trim is not working in VBA?

for i = 3 to 2000
activesheet.cells(i,\"C\").value = trim(Activesheet.cells(i,\"C\").value)
next i

It is un

10条回答
  •  难免孤独
    2021-01-13 00:46

    Trim removes extra spaces at start and end, not in the middle of a string.

    Function CleanSpace(ByVal strIn As String) As String
        strIn = Trim(strIn)
    
      ' // Replace all double space pairings with single spaces
        Do While InStr(strIn, "  ")
            strIn = Replace(strIn, "  ", " ")
        Loop
    
        CleanSpace = strIn
    End Function
    

    From here.

    PS. It's not the most efficient way to remove spaces. I wouldn't use on many, very long strings or in a tight loop. It might be suitable for your situation.

提交回复
热议问题