File name to appear in cell when workbook opened

russelldt

Board Regular
Joined
Feb 27, 2021
Messages
158
Office Version
  1. 365
Platform
  1. MacOS
I am working with excel 365. Files are accessed online using One Drive

I would like to insert the file name (no path) in a particular cell, when I open the file

Can anyone suggest a solution. @filenamemdoes not work

Thank you
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try the following in the Workbook module of the file in question. Change the sheet/address to suit:

VBA Code:
Private Sub Workbook_Open()
    Sheet1.Range("A1") = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1)
End Sub
 
Upvote 0
IMO you need to use the workbook open event. Off the top of my head, something like
VBA Code:
Private Sub Workbook_Open()
Sheets("Sheet1").Range("A1") = ThisWorkbook.Name
End Sub
Not sure it makes sense to do this every time you open the workbook seeing as how the value will already be there the first time you open the wb.
Change your sheet and cell references of course.
 
Upvote 0
Try the following in the Workbook module of the file in question. Change the sheet/address to suit:

VBA Code:
Private Sub Workbook_Open()
    Sheet1.Range("A1") = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1)
End Sub

Try the following in the Workbook module of the file in question. Change the sheet/address to suit:

VBA Code:
Private Sub Workbook_Open()
    Sheet1.Range("A1") = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".") - 1)
End Sub
Yep, that worked, thanks.
 
Upvote 0
Another option using a formula
Excel Formula:
=TEXTBEFORE(TEXTAFTER(CELL("filename",A1),"[",-1),"]",-1)
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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