Rename Worksheet with Delimited Workbook name

Poppaby

New Member
Joined
Nov 19, 2020
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi All

I have a folder with a number of workbooks. Each file has 1 tab called "sheet 1".

Each workbook has a long complex name in the form of xxxxx_xxx_xxxx_xxxxxx_y_xxxxxxxx_x

where the x is a set length on every workbook, but the y is a random length in each file

I would like to rename "sheet 1" in every workbook in a set folder to "y"

Is there a way to use the "_" delimiter to create a macros for this purpose?

Additionally, is there a way to then expand on this result to further amend the name - like a recurring if function so that

if "y" = Apples then rename the worksheet RED_Apples

Thanks in advance for any help you can provide

Dave
 

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
Hi & welcome to MrExcel.
Will the "y" always be the 5th element of the workbook name?
 
Upvote 0
Thank you - yes Y is always in the same position between delimiter 4 and 5
 
Upvote 0
Ok, how about
VBA Code:
Sub Poppaby()
   Dim Pth As String, Fname As String
   Dim Sp As Variant
   Dim Wbk As Workbook
   
   Pth = "C:\MrExcel\Fluff\"
   Fname = Dir(Pth & "*.xls*")
   Do While Fname <> ""
      Set Wbk = Workbooks.Open(Pth & Fname)
      Sp = Split(Fname, "_")
      Wbk.Sheets("Sheet 1").Name = Sp(4)
      Wbk.Close True
      Fname = Dir
   Loop
End Sub
Change the Pth to suit
 
Upvote 0
Thank you

This seems to change the sheet name to xxxxx_xxx_xxxx_xxxxxx_y
cutting off the name when the character limit is reached.

I need it to be y only if that is possible
 
Upvote 0
Did you copy/paste the code into a module, or re-type it?
 
Upvote 0
In that case something funny is going on, as there is no way that code can just take the 1st 31 characters of the filename.
Did you get any error messages?
 
Upvote 0
I think I see my error - the problem isnt as complicated as I first thought

So the work book name is xxxxx_xxx_xxxx_xxxxxx_y_xxxxxxxx_x

However the first sheet is randomly generated name - I just need a macros that will rename the sheet to Y

Thanks again
 
Upvote 0
Is it the only sheet in the workbook?
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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