Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove file extension from file name (VBA)

Tags:

excel

vba

I have a filename variable that contains : "Filename.csv" . To extract the filename from a path I use: Filename=Dir([fStr]) where fStr is retrieved from the file that I selected.

I only need the filename without ".csv". How do I remove the ".csv" extension?

like image 965
F1990 Avatar asked Aug 15 '15 11:08

F1990


3 Answers

It's best to use a function like GetBaseName() instead of relying on functions to replace text. Windows allows periods to appear within the base filename so something like this is legitimate:

My .csv for Bob.csv

Using Replace() would result in:

My  for Bob

Not what you're looking for. A better approach would be:

Filename = CreateObject("Scripting.FileSystemObject").GetBaseName(fStr)
like image 174
Bond Avatar answered Oct 20 '22 22:10

Bond


You can use the replace function:

Filename = replace(Dir([fStr]),".csv","")
like image 35
Philippe Grondier Avatar answered Oct 20 '22 23:10

Philippe Grondier


Too late to answer it, might be helpful for future

Try this

Mid(fileName, 1, InStr(1, fileName, ".") - 1)

It will work with any file extension or filename

For example:

fileName : ThisIsMyFile.csv

like image 3
The Automation Techies Avatar answered Oct 21 '22 00:10

The Automation Techies