Consulting

Results 1 to 7 of 7

Thread: Need help with vba code for deleting trailing spaces before or after an entry in a co

  1. #1
    VBAX Regular
    Joined
    Jan 2008
    Posts
    75
    Location

    Unhappy Need help with vba code for deleting trailing spaces before or after an entry in a co

    Hi All,

    I do have a vba code that I wrote that validate length of entries in a column, and is working, but does not check if there is a trailing space before or after the entry. Please, I would be grateful if anyone in this forum will help me out.

    Thanks

    Lucpian

  2. #2
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    It sounds like you just need to use the Trim function.
    [VBA]NewEntryString = Trim(OldEntryString)[/VBA]

  3. #3
    VBAX Regular
    Joined
    Jan 2008
    Posts
    75
    Location
    Here is the code
    [VBA]
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' checklength
    ' This is part of the fields Validation calling function
    ' which check for length of entries in cells in the Worksheet, and colors it
    ' yellow.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function checklength(columnname As Integer, length As Integer)
    Dim rowcount
    Dim R
    rowcount = Range("A65536").End(xlUp).Row
    For R = 2 To rowcount
    strVal = Sheet1.Cells(R, columnname).Value
    If strVal <> "" And (Not (Len(strVal) = length)) Then
    Sheet1.Cells(R, columnname).Interior.ColorIndex = 6
    End If
    Next

    End Function
    [/VBA]
    My question is where do I place this trim code to indicate that I do not want a trailing space before or after?

    Thanks

    Lucpian

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Lucpian,
    When you post code, please select it and click the VBA button to format it as shown.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    Try this:
    [VBA]''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' checklength
    ' This is part of the fields Validation calling function
    ' which check for length of entries in cells in the Worksheet, and colors it
    ' yellow.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function checklength(columnname As Integer, length As Integer)
    Dim rowcount
    Dim R
    rowcount = Range("A65536").End(xlUp).Row
    For R = 2 To rowcount
    strVal = Trim(Sheet1.Cells(R, columnname).Value)
    If strVal <> "" And (Not (Len(strVal) = length)) Then
    Sheet1.Cells(R, columnname).Interior.ColorIndex = 6
    End If
    Next
    End Function
    [/VBA]

    I'm slightly unclear about what you really want, so this is just a guess.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Function checklength(columnname As Integer, length As Integer)
    Dim rowcount
    Dim R
    rowcount = Range("A65536").End(xlUp).Row
    For R = 2 To rowcount
    strVal = Sheet1.Cells(R, columnname).Value
    If Trim(strVal) <> "" And Len(Trim(strVal)) <> length Then
    Sheet1.Cells(R, columnname).Interior.ColorIndex = 6
    End If
    Next
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Note that Trim will not handle non-printing characters such as Chr(160) which can occur in imported data.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •