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?
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!