Need help naming a worksheet based off another worksheet in VBA

codys21

New Member
Joined
Jun 12, 2015
Messages
19
Right now I have two sheets, named Sept 2014 Raw Data and Sept 2013 Raw Data. I am taking the raw data and filtering it using VBA and putting the filtered data into a new worksheet. What I would like to be able to do is name the new worksheet based on where the data came from. For instance, if I ran my code on the Sept 2014 Raw Data worksheet, I would like my new sheet to be named Sept 2014, likewise for the Sept 2013 Raw Data. So, if there's some sort of code which can name the sheet from the first x amount of characters (in this case 9) that would be fantastic.

Thanks for the help!
Cody
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Something like this:

Code:
Sub SheetName()
    
    Sheets.Add.Name = Left(ActiveSheet.Name, 9)
    
End Sub
 
Upvote 0
Something like this:

Code:
Sub SheetName()
    
    Sheets.Add.Name = Left(ActiveSheet.Name, 9)
    
End Sub


This doesn't work because I need to use the left 9 characters of the sheet to the right of the active sheet. So I need to reference the raw data sheet and use the left 9 characters of that to name the newly added active sheet.
 
Upvote 0
This doesn't work because I need to use the left 9 characters of the sheet to the right of the active sheet. So I need to reference the raw data sheet and use the left 9 characters of that to name the newly added active sheet.

This would need to be done before you add the sheet. Try running just my code while you have the Sept 2014 Raw Data sheet active.
 
Upvote 0
This doesn't work because I need to use the left 9 characters of the sheet to the right of the active sheet. So I need to reference the raw data sheet and use the left 9 characters of that to name the newly added active sheet.

If you really want it to use the sheet to the right of the active sheet, then:

Code:
Sheets.Add.Name = Left(Sheets(ActiveSheet.Index + 1).Name, 9)
 
Upvote 0
This would need to be done before you add the sheet. Try running just my code while you have the Sept 2014 Raw Data sheet active.

It still doesn't work because it is adding a new sheet and then trying to name it based off of this new sheet. So, it's taking "sheet3" and trying to get the 9 leftmost characters and that's not working.
 
Upvote 0
If you really want it to use the sheet to the right of the active sheet, then:

Code:
Sheets.Add.Name = Left(Sheets(ActiveSheet.Index + 1).Name, 9)


Even with that I am getting the error "That name is already taken. Try a different one."
 
Upvote 0
It still doesn't work because it is adding a new sheet and then trying to name it based off of this new sheet. So, it's taking "sheet3" and trying to get the 9 leftmost characters and that's not working.

Why are you running the code on Sheet3? Of course it won't work on Sheet3 since the left 9 (or in this case 6) characters are Sheet3, which already exists.

As I said in my previous post, try it on Sept 2014 Raw Data. The newly added sheet doesn't become the active sheet until the full line of code is executed. Therefore, Sept 2014 Raw Data is the active sheet prior to adding and naming the new sheet.

This code works on my machine.....
 
Upvote 0

Forum statistics

Threads
1,203,146
Messages
6,053,755
Members
444,681
Latest member
Nadzri Hassan

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