Trim Cells using VBA in Excel

后端 未结 8 1163
你的背包
你的背包 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:28

    If you have a non-printing character at the front of the string try this

    
    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 < 33 Or nAscii = 160 Then
                        If Len(str) > 1 Then
                            str = Right(str, Len(str) - 1)
                        Else
                            strl = ""
                        End If
                    End If
                End If
                cell=str
            End If
        Next
    End Sub
    

提交回复
热议问题