Consulting

Results 1 to 14 of 14

Thread: Solved: Trim Function...

  1. #1
    VBAX Regular
    Joined
    May 2007
    Posts
    13
    Location

    Solved: Trim Function...

    I saw that there was a code submitted by Anne Troy that Changed the case of selected cells.
    [vba]
    Option Explicit

    Sub ConvertCase()
    Dim Rng As Range
    For Each Rng In Selection.Cells
    If Rng.HasFormula = False Then
    'Use this line for UpperCase text; change UCase to LCase for LowerCase text.
    Rng.Value = UCase(Rng.Value)
    End If
    Next Rng
    End Sub [/vba]
    I saw that you could change the text from upper to lower case by just changing " Rng.Value = UCase(Rng.Value) "

    I would like to summon the trim function instead of lower case or upper case for the selected text.
    Or even better combine the two Upper case and Trim.

    Thanks in adavance.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Flipback,
    Welcome to VBAX
    try
    [VBA] Rng.Value = UCase(Trim(Rng.Value)) [/VBA]
    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'

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    BTW, when you post code, 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'

  4. #4
    VBAX Regular
    Joined
    May 2007
    Posts
    13
    Location
    That worked great. Now what would I do if I just wanted the trim function?

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

    Rng.Value = Trim(Rng.Value)
    [/vba]

  6. #6
    VBAX Regular
    Joined
    May 2007
    Posts
    13
    Location
    Compile error:
    Wrong number of arguments or invalid property assignment
    ___________________________________________________________

    Sub Trim()
    Dim Rng As Range
    For Each Rng In Selection.Cells
    If Rng.HasFormula = False Then
    Rng.Value = Trim(Rng.Value)
    End If
    Next Rng
    End Sub

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You can't call your sub trim, otherwise that overrides the real Trim function, and it tries to call itself.

  8. #8
    VBAX Regular
    Joined
    May 2007
    Posts
    13
    Location
    Thanks for the help. Everything worked out great. =)

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Flipback,
    If this is Solved, you can mark it so using the Tread Tools dropdown
    Regards
    MD
    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'

  10. #10
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    You should explicitly call which Trim function you want. Either VBA.Trim or Worksheetfunction.Trim:

     
    Sub TEST()
    MsgBox VBA.Trim("   Malik   641   ") ' Results "Malik   641"
    MsgBox WorksheetFunction.Trim("   Malik   641   ") ' Results "Malik 641"
    End Sub
    Plane old Trim with no explicit call results in VBA.Trim. FYI




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why? If you take that attitude, you should also apply it to VBA.Left, VBA.Len, etc.

  12. #12
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    I've just been in that mode lately. Like Excel.Range and Public Sub, rather than relying on defaults. I don't know if they have any gains, I just like it better. It's clearer, even though it takes longer to code.

    ...Maybe I shouldn't have said "You should explicitly call...". It's a preference, really. I haven't found problems with defaults.

    And the OP should know that Trim will default to VBA.Trim and that they will have to call for Worksheetfunction.Trim for different results.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I tend toi agree with that in principle, for instance I always use Private and Public in procedure signatures, but using VBA seems like taking it too far even for me.

  14. #14
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    I see your point. But like I said I'm just in a mode of doing this for now. I don't know if I'll stick with it forever. A big part of the reason I'm doing it is to get familiar with VBA.methods




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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