+ Reply to Thread
Results 1 to 17 of 17

Need cmd to trim the end of text in a textbox

  1. #1
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Need cmd to trim the end of text in a textbox

    Hi all, I made a command button to paste some copied text from a worksheet1 to a userform textbox in another worksheet2.

    Here's my simple code:

    Private Sub CommandButton1_Click()
    TxtBoxProduct.Paste
    End Sub

    It works fine but I always have to get to the end of the textbox text after that in order to remove any blank spaces as it destroys my worksheet2 format (i.e. borders etc).

    Is there a "trim" function I could add to my code?
    As you can see, I'm kinda new with vba...

    Many thanks in advance for your help!
    Regards
    Leweez

  2. #2
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Need cmd to trim the end of text in a textbox

    Hi. Show an example.
    To do it for me and help me it is 2 different things!
    Sorry for my english, blame Google translator

  3. #3
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need cmd to trim the end of text in a textbox

    What can I show as an example?

  4. #4
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Need cmd to trim the end of text in a textbox

    for example, the file with your comboboxes

  5. #5
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need cmd to trim the end of text in a textbox

    It's not a combobox, it's a plain Textbox.

  6. #6
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Need cmd to trim the end of text in a textbox

    what difference, the main thing is to see what happens

  7. #7
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need cmd to trim the end of text in a textbox

    I don't know what to show you but this is what I want to do:

    1- I Copy plain text from a worksheet
    2- I want to paste this text into another worksheet in a userform textbox.
    3- It actually does copy perfecty the text but for some unknown reason, it must have some "blank spaces" while pasting the text because it messes up the formatting of my worksheet2.

    4- If I go at the end of my "pasted" text before closing my userform and do a "delete", then the formatting of worksheet2 is Ok.

    I would want to avoid doing this last step as it is not very practical.
    Last edited by Leweez; 03-15-2016 at 03:54 PM.

  8. #8
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Need cmd to trim the end of text in a textbox

    became not clearer but here is the code with "trim"
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need cmd to trim the end of text in a textbox

    Unfortunately, it doesn't work. My paste text is still messing with the format of my "receiving" worksheet.
    Sorry that I cannot make my question more clearer.

    Is there a better way to have the mouse click contextmenu to "cut/copy/paste" in a vba userform ?
    Last edited by Leweez; 03-15-2016 at 04:22 PM.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Need cmd to trim the end of text in a textbox

    Leweez

    Do you actually have a userform with a textbox or are you referring to a textbox on a worksheet?

    If it's the latter is it a textbox from the Forms or ActiveX toolbar?

    Also, are you just trying to get the value from a worksheet into a textbox?

    To do that you don't need to paste.

    kalbasiatka

    Why not use VBA's Trim?
    If posting code please use code tags, see here.

  11. #11
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Need cmd to trim the end of text in a textbox

    Why not use VBA's Trim?
    vba trim removes only the last spaces

  12. #12
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need cmd to trim the end of text in a textbox

    As I mentionned in my post, I have a userform with several textbox that places everything from that userform to the worksheet.
    I try to copy/paste some text from another worksheet in that userform textbox but it's not working.

    How can we right click on a userform and have a context menu?

  13. #13
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Need cmd to trim the end of text in a textbox

    here is an example with a form
    Attached Files Attached Files

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Need cmd to trim the end of text in a textbox

    Leweez

    If you want to put the value from a cell in a textbox why paste?
    Please Login or Register  to view this content.
    Vice versa, textbox to sheet.
    Please Login or Register  to view this content.

    kalbasiatka

    VBA Trim removes trailing and leading spaces.

    The worksheet function TRIM removes trailing/leading spaces and replaces multiple spaces between text with single spaces.

  15. #15
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need cmd to trim the end of text in a textbox

    [QUOTE=Norie;4339780]Leweez

    If you want to put the value from a cell in a textbox why paste?
    Please Login or Register  to view this content.
    Vice versa, textbox to sheet.
    Please Login or Register  to view this content.

    Because the value of a cell comes from another workbook and I have to copy/paste several cells to my userform textbox located in another workbook.
    My error here, I wrote "2 different worksheets" but it's really 2 different workbook.

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Need cmd to trim the end of text in a textbox

    It doesn't matter, just include reference(s) to the relevant workbook(s) as well as the worksheet(s) in the code I posted.

    For example.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    03-15-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need cmd to trim the end of text in a textbox

    Thank you!

    Regards
    Leweez

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Display Full Text within Textbox (On A UserForm) when you hover over the textbox
    By ShakJames in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-20-2015, 09:01 AM
  2. copy text from userform textbox to sheet textbox
    By Dfrost in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-28-2015, 02:19 AM
  3. Trim text after -
    By TamiT in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-13-2015, 10:17 AM
  4. add a string of locked text to vba textbox based on entry in the same textbox
    By yoursamrit2000 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-21-2014, 11:48 PM
  5. [SOLVED] Trim text
    By rizmomin in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-19-2013, 11:18 AM
  6. VBA - Macro Code to copy textbox text to another worksheet textbox
    By nitram lowm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-05-2013, 08:39 AM
  7. Highlight all Text in a Textbox when the textbox is selected
    By RPIJG in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2005, 03:28 PM

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