+ Reply to Thread
Results 1 to 8 of 8

VBA code to delete the space at the first or end and one of tow spaces together

  1. #1
    Registered User
    Join Date
    08-16-2011
    Location
    Tehran,Iran
    MS-Off Ver
    Excel 2007
    Posts
    25

    VBA code to delete the space at the first or end and one of tow spaces together

    HI

    I have a user form to save data.
    Is there Any way to delete the spaces that the user add at the first or end of the data in a text box? Or don't save this spaces in cell?

    and

    is there any way to delete all spaces between two words that are typed in text boxes except one? Or don't save them in cell?


    thank you,

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA code to delete the space at the first or end and one of tow spaces together

    s_samira_21,

    What you're looking for is the Trim() operator.
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    08-16-2011
    Location
    Tehran,Iran
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: VBA code to delete the space at the first or end and one of tow spaces together

    thank you very much

    but unfortunately I don't Know how to use this code
    I used this but it doesn't work.

    ws.Cells(iRow, 1).Value = Trim(Me.txtCode.Value)

    could you please help me?

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA code to delete the space at the first or end and one of tow spaces together

    s_samira_21,

    Instead of textbox.value, use textbox.text:

    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

  5. #5
    Registered User
    Join Date
    08-16-2011
    Location
    Tehran,Iran
    MS-Off Ver
    Excel 2007
    Posts
    25

    Unhappy Re: VBA code to delete the space at the first or end and one of tow spaces together

    thank you

    in works for spaces at the first and end of the words
    but it doesn't work for spaces between words

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA code to delete the space at the first or end and one of tow spaces together

    s_samira_21,

    Ah, I see. That's what I get for not testing it. I created a UDF that can be used to remove all extra spaces:
    Please Login or Register  to view this content.


    To use it, just feed a variable into it:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

  7. #7
    Registered User
    Join Date
    08-16-2011
    Location
    Tehran,Iran
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: VBA code to delete the space at the first or end and one of tow spaces together

    Hi again

    thank you

    I put first part in top of the form code
    but I don't know where should I use the second part

    sorry I almost don't know anything about VBA

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA code to delete the space at the first or end and one of tow spaces together

    the TRIM function in VBA is different from the Worksheet function TRIM(). You can use either. I think the OP's need is the worksheet version of the TRIM().

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1