Renaming sheet names depending on file name

Tej92

Board Regular
Joined
Sep 27, 2022
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Hello everyone, I'm trying to rename the sheet1 to the first 2 words of the file name. I've managed to do it by the following formula in cell C1 to get the file name:=LEFT(MID(CELL("filename"),FIND("[",CELL("filename"))+1, FIND("]",CELL("filename"))-FIND("[",CELL("filename"))-1),FIND(".",MID(CELL("filename"),FIND("[",CELL("filename"))+1, FIND("]",CELL("filename"))-FIND("[",CELL("filename"))-1))-1)

then this formula to get the first 2 words:=TRIM(LEFT(C1, FIND("^",SUBSTITUTE(C1, " ", "^",2)&"^")))

and then the following VBA code to rename the sheet:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("AD1")) Is Nothing Then

ActiveSheet.Name = ActiveSheet.Range("AD1")

End If

End Sub


But it doesn't change the sheet name unless i go to AD1 and click enter. the value in AD1 is correct as soon as i open the file but it doesn't change the sheet name unless enter is pressed inside the formula tab.
I'd like it to change the name of the cell with out that extra step. any idea?
 
@Fluff that formula in AD1 works as it should but the formula for C1 is also giving the file extension. that's the formula i found online works fine even though it's quite long. So far the performance of the sheet is not altered.
thank you for the help!!
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
@Fluff, might it have something to do if their Windows setting is toggled to show/not show file extensions?
 
Upvote 0
Missed the fact you don't want the extension, so it should be
Excel Formula:
=TEXTBEFORE(TEXTAFTER(CELL("filename",A1),"["),".",-1)
 
Upvote 1
that is a lot shorter of a code and works perfectly!!! thanks!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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