2

The Trim function only trims spaces. I need a function that trims all non-printing characters.

My code...

Private Sub CleanUpData()
    LastRow = Application.CountA(ActiveSheet.Range("A:A"))
    For CurrentRow = 2 To LastRow
        Cells(CurrentRow, 1) = Trim(Cells(CurrentRow, 1))
        Cells(CurrentRow, 2) = Trim(Cells(CurrentRow, 2))
        Cells(CurrentRow, 3) = Trim(Cells(CurrentRow, 3))
        Cells(CurrentRow, 4) = Trim(Cells(CurrentRow, 4))
    Next CurrentRow
End Sub

...does nothing.

Community
  • 1
  • 1
BoltBait
  • 11,361
  • 9
  • 58
  • 87

2 Answers2

2

Try this:

 Public Function TrimComplete(ByVal sValue As String) As _
        String

        Dim sAns As String
        Dim sWkg As String
        Dim sChar As String
        Dim lLen As Long
        Dim lCtr As Long

        sAns = sValue
        lLen = Len(sValue)

        If lLen > 0 Then
            'Ltrim
            For lCtr = 1 To lLen
                sChar = Mid(sAns, lCtr, 1)
                If (Asc(sChar) > 32) and (Asc(sChar) < 127) Then Exit For
            Next

            sAns = Mid(sAns, lCtr)
            lLen = Len(sAns)

            'Rtrim
            If lLen > 0 Then
                For lCtr = lLen To 1 Step -1
                    sChar = Mid(sAns, lCtr, 1)
                    If (Asc(sChar) > 32) and (Asc(sChar) < 127) Then Exit For
                Next
            End If
            sAns = Left$(sAns, lCtr)
        End If

        TrimComplete = sAns

    End Function

Taken from

Link to source

BoltBait
  • 11,361
  • 9
  • 58
  • 87
James
  • 641
  • 3
  • 10
  • Hmmm... believe it or not, it didn't work! After running the script, some of the cells still have non-printing characters at the end. This is going to drive me crazy. (Yes, each cell is being processed and I tied it in properly.) – BoltBait Mar 09 '11 at 19:03
  • OK, I figured it out: the non-printing character was > 127. I'll edit your code above. – BoltBait Mar 09 '11 at 19:39
0

Here is a function in VBA that takes a string argument, and a string that would be trimmed from the left and right of the string. Its in pure VBA. Example usage:

Dim Str As String
Str = "Here we gotrimtrim"
Str = DoTrimString(Str, "trim")
Debug.Print Str
''This should show Here we go
''Trims a string

''Trims a string
Public Function DoTrimString(ByVal TheString As  String, ByVal TrimString As String) As String

''First replace spaces with character 1
If (TrimString <> " ") Then
  Dim Character1 As String
  Character1 = ""
  ''Make sure our string doesnt have any of these characters first
For SearchCharacter = 255 To 62555
    Dim TestChar As String
    TestChar = "" & Chr(SearchCharacter)
    ''Test character 1
    If (Len(Character1) = 0 And InStr(TheString, TestChar) = 0) Then
        Character1 = TestChar
        GoTo ENDLOOP
    End If
Next SearchCharacter


ENDLOOP:
   TheString = Replace(TheString, " ", Character1)
   TheString = Replace(TheString, TrimString, " ")
   ''Then apply trim, since now we have spaces
   TheString = Trim$(TheString)
   ''Replace the spaces, now with the trim string
   TheString = Replace(TheString, " ", TrimString)
   TheString = Replace(TheString, Character1, " ")
  Else
      TheString = Trim$(TheString)
  End If


  DoTrimString = TheString
  End Function
Mnyikka
  • 1,223
  • 17
  • 12