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!'
Consider having the VBA code save the copied text to a temp.txt file. Then have your VBA code look for and eliminate all spaces. You can use the ASCII code for space Chr(32).
Something like this...
'Begin edit Temp
Dim TextFile As Integer Dim FilePath As String Dim FileContent As String
'File Path of Text File FilePath = "C:\VBAtemp\temp.txt" 'Use a location of your choosing.
'Determine the next file number available for use by the FileOpen function TextFile = FreeFile
'Open the text file in a Read State Open FilePath For Input As TextFile
'Store file content inside a variable FileContent = Input(LOF(TextFile), TextFile)
'Clost Text File Close TextFile
'Find/Replace FileContent = Replace(FileContent, Chr(32), "") 'This will remove the spaces.
'Open the text file in a Write State Open FilePath For Output As TextFile
'Write New Text data to file Print #TextFile, FileContent
'Close Text File Close TextFile
'End edit Temp
Hope this helps!