ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Chopping the last 4 characters of a string in VBA (https://www.excelbanter.com/excel-programming/348700-chopping-last-4-characters-string-vba.html)

silkworm

Chopping the last 4 characters of a string in VBA
 
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


42N83W

Chopping the last 4 characters of a string in VBA
 

"silkworm" wrote in message
ups.com...
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-



Ron Rosenfeld

Chopping the last 4 characters of a string in VBA
 
On 21 Dec 2005 18:05:37 -0800, "silkworm" wrote:

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

silkworm

Chopping the last 4 characters of a string in VBA
 
Thank you very much, guys.



All times are GMT +1. The time now is 05:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com