Textbox1 | Remove Empty Characters

Hello Again 

Quick question... 

I have a text box i'm using to find stuff ... Basically put in my word and click a command button to find it. But I'm having to manually remove empty characters before or after my text (mostly only after i copy and paste my phrase in the textbox) to search. How do I set my vba code up to recognize there is a space before and/or after my text and remove it?

Example: ( _ = space)

Find word "Apple"

But when i copy & past "Apple" in my Textbox there will be a space before or after "Apple" and the results cannot be found. "_Apple", "Apple_" or "_Apple_"  

So how do I remove empty characters upon clicking my command button?

  • Just though of this... 

    Or when I copy my text in the textbox (the textbox "change" ) will auto adjust my text... 

  • Look up the VBA string functions. There are a bunch of them.

    LTrim, RTrim, and Trim might be of particular interest to you.

  • Hi,

    If 'Trim()' solves your problem, it's nice. I don't know exactly where you get your 'Apple' from, it may happen, that Trim() does not work, because not all what is looking like a space is indeed a space. Very mean are tabulators and especially a mix of them together with spaces. To delete them as well you should use MyTxt=Replace(MyTxt, Chr(9), "") before you execute MyTxt = Trim(MyTxt).

    You wrote as well, that you paste a string into a textbox to feed the macro with your input. I don't know, if this is a workaround because you don't know the 'InputBox()'. If you need this information also for your output or have other reasons to fill this textbox, your solution may be fine. If you want to know something more about ways to enter a value into a macro while it's running, feel free to have a look to the discussion I started a few minutes ago with the caption ' How to: Enter a parameter to a macro while it's running!'