Chopping the last 4 characters of a string in VBA

S

silkworm

Hi, is there is a way to chop off the last 4 characters of a string?

I have a group of text file names and I want to delete the ".txt" part
at the end of the names.

Thanks
 
4

42N83W

silkworm said:
Hi, is there is a way to chop off the last 4 characters of a string?

I have a group of text file names and I want to delete the ".txt" part
at the end of the names.

Thanks

If cell A1 contains a text value you want to parse, use this

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

HTH

-gk-
 
R

Ron Rosenfeld

Hi, is there is a way to chop off the last 4 characters of a string?

I have a group of text file names and I want to delete the ".txt" part
at the end of the names.

Thanks


Since you're in the programming group, here is a VBA example:

Sub foo()
Const str As String = "foobar.txt"
Debug.Print Replace(str, ".txt", "")
End Sub

The above is case sensitive. If you want to do a case insensitive replacement,
then:

Option Compare Text
Sub foo()
Const str As String = "foobar.TXT"
Debug.Print Replace(str, ".txt", "")
End Sub


If you are doing this on a worksheet:

=SUBSTITUTE(A1,".txt","")

=REPLACE(A1,SEARCH(".txt",A1),4,"")

The SUBSTITUTE function is case sensitive. The other is not.


--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top