Add File Name to a Cell without Extension?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
915
Office Version
  1. 365
  2. 2019
Hi all,

I've seen a few ways to do this, but they don't account for file extensions being different lengths - typically 3 (.xls) or 4 (.xlms)

Is there anyway this can be done? My file names will never have a period in them if that helps.

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi all,

I've seen a few ways to do this, but they don't account for file extensions being different lengths - typically 3 (.xls) or 4 (.xlms)

Is there anyway this can be done? My file names will never have a period in them if that helps.

Thanks
In any empty cell in any worksheet in your file enter:
Code:
=MID(CELL("filename",A1),SEARCH("[",CELL("filename",A1))+1,SEARCH(".",CELL("filename",A1))-1-SEARCH("[",CELL("filename",A1)))
 
Upvote 0
Thank you so much! Curious - what does the A1 reference actually do? Doesn't seem to have any impact on the result?
 
Upvote 0
Upvote 0
Thank you so much! Curious - what does the A1 reference actually do? Doesn't seem to have any impact on the result?

A1 is simply a reference to a cell on the sheet that contains the formula, it can be any cell on the sheet, but if omitted the default will be the last cell that was changed. In your case, if that cell happened to be in another file you would not get the result you want so best not to omit it. By default, I always use A1.
 
Upvote 0
Sorry to drag this back up.

I'm trying to use this formula but it looks like my server doesn't include the '[]' at either end of the filename, and so the formula fails. This is using Excel 2016 and saving the files as .csv

Is there something obvious I'm missing?

Thanks!
 
Upvote 0
Sorry to drag this back up.

I'm trying to use this formula but it looks like my server doesn't include the '[]' at either end of the filename, and so the formula fails. This is using Excel 2016 and saving the files as .csv

Is there something obvious I'm missing?

Thanks!
What does the formula: =CELL("filename",A1) return?
 
Upvote 0
What does the formula: =CELL("filename",A1) return?

I'll give a bit of background in the hope that this might help...

I am creating templates to use for uploading data into our database, and the templates are saved as .xlsx format. Before uploading to the database, they need to be saved as a .csv file. When in .xlsx format, the formula works and the formula you've mentioned shows a filename with '[]' in. When saving as a .csv, the '[]' disappear and so the original formula errors.

So, it looks like I was wrong with my original idea that it was something to do with the server and it's actually more to do with the file format. Is there a way round this for .csv files?
 
Upvote 0
I'll give a bit of background in the hope that this might help...

I am creating templates to use for uploading data into our database, and the templates are saved as .xlsx format. Before uploading to the database, they need to be saved as a .csv file. When in .xlsx format, the formula works and the formula you've mentioned shows a filename with '[]' in. When saving as a .csv, the '[]' disappear and so the original formula errors.

So, it looks like I was wrong with my original idea that it was something to do with the server and it's actually more to do with the file format. Is there a way round this for .csv files?
This should work for your csv file, but could probably be done in a way that requires far fewer function calls. It's just the first apporach that comes to mind for me.
Code:
=MID(SUBSTITUTE(CELL("filename",A1),"\","[",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))),SEARCH("[",SUBSTITUTE(CELL("filename",A1),"\","[",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))+1,SEARCH(".",CELL("filename",A1))-SEARCH("[",SUBSTITUTE(CELL("filename",A1),"\","[",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))-1)
Basically, this substitutes "[" for the last backslash character that comes before the file name, and uses that marker and the "." before the csv extension to isolate just the file name.
 
Last edited:
Upvote 0
This should work for your csv file, but could probably be done in a way that requires far fewer function calls. It's just the first apporach that comes to mind for me.
Code:
=MID(SUBSTITUTE(CELL("filename",A1),"\","[",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))),SEARCH("[",SUBSTITUTE(CELL("filename",A1),"\","[",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))+1,SEARCH(".",CELL("filename",A1))-SEARCH("[",SUBSTITUTE(CELL("filename",A1),"\","[",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"\",""))))-1)
Basically, this substitutes "[" for the last backslash character that comes before the file name, and uses that marker and the "." before the csv extension to isolate just the file name.

Thanks very much, I'll give this a go!
 
Upvote 0

Forum statistics

Threads
1,223,641
Messages
6,173,505
Members
452,517
Latest member
SoerenB

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top