Insert File name in cell

russelldt

Board Regular
Joined
Feb 27, 2021
Messages
158
Office Version
  1. 365
Platform
  1. MacOS
Hello,

I want to insert a file name in a cell when i open a file. This is what I am currently using:

=MID(CELL("filename"),SEARCH("[",CELL("filename"))+1, SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))-5)

The -5 at the end removes the elm extension from the displayed name

This works when I open the file, directly from Finder. However, as the file is linked to a master file when I open this file using the link in the master file, I don't get the file name, It shows part of the directory structure.

Any help please
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Are Macros/VBA okay? If so you could try writing a function that uses Thisworkbook.Name and returns the value. Then you can use it as a function in the formula line, which should only care about what the file's name is.
 
Upvote 0
I am not up to scratch with macros- hence my attempt with this.

A macro would be very much appreciated. Thanks
 
Upvote 0
Go to your VBA editor, add a module to the VBA Project and paste:
VBA Code:
Function FileName() As String
    FileName = ThisWorkbook.Name
End Function
Now you can go into your workbook, select a cell, and type =FileName()
 
Upvote 0
Go to your VBA editor, add a module to the VBA Project and paste:
VBA Code:
Function FileName() As String
    FileName = ThisWorkbook.Name
End Function
Now you can go into your workbook, select a cell, and type =FileName()
Thanks very much. Such a simple solution.
 
Upvote 0
Thanks very much. Such a simple solution.

When i "Save As" the excel file, with a new File name, it retains the result from the original Filename(). Ie the original file name is xxx123, and when i save it as xxx124, the xxx123 appears in the cell in the xxx124 file.
 
Upvote 0
Fix for you:
VBA Code:
Function FileName() As String
    Application.Volatile
    FileName = ThisWorkbook.Name
End Function
This should update after something changes on the sheet post name change.
 
Upvote 0
Fix for you:
VBA Code:
Function FileName() As String
    Application.Volatile
    FileName = ThisWorkbook.Name
End Function
This should update after something changes on the sheet post name change.
Thanks, but still the same issue occurs.
 
Upvote 0
thanks , managed to get this to work - by deleting the initial script, and starting with the revised one you sent. I think i was running both at the same time.
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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