MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Delete part of a string?


Posted by Chris M on December 05, 2001 1:49 PM

I have a string that is a file name. "010416.sta" I want to delete the last four characters of the string so I can save it as a .txt without the extension still being .sta. Is there a way to use (right(string,4).delete or something? I can't go from the left because most of my filenames are not the same length. I need to be able to use this in a vba macro.


Posted by Aladin Akyurek on December 05, 2001 1:52 PM

=SUBSTITUTE(A1,RIGHT(A1,4),"")

where A1 houses the original string.

Aladin

Posted by Juan Pablo G. on December 05, 2001 1:57 PM

Or in VBA two options.

First, provided by Aladin

str = Application.Substitute(str,Right(str,4),"")

or

str = Left(str,Len(str)-4)

(You can't use String as a variable name)

Juan Pablo G.

Posted by Chris M. on December 05, 2001 2:01 PM

Works great. Thanks guys.