Changing Tab Names VBA

Tom98

New Member
Joined
Oct 26, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi,

If I wanted to go through each sheet and rename the tab names as the previous sheet plus 1, how would I go about doing this? Ie if I had sheet1, sheet2, sheet3 and sheet4, and then I were to delete sheet3 I would then like a macro to run to update the remaining sheets as sheet1, sheet2, sheet3. Any help with this would be great!

TIA
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,307
The problem is that if it starts at 1 and Sheets("Test_1") already exists, the macro will generate an error because you can't have two sheets with the same name.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Tom98

New Member
Joined
Oct 26, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
The problem is that if it starts at 1 and Sheets("Test_1") already exists, the macro will generate an error because you can't have two sheets with the same name.
Okay, there wouldn't be a way of telling it to skip over the sheets that I don't want it to consider so to start it counting at 1 and going from there?

Sorry for the hassle.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,307
there wouldn't be a way of telling it to skip over the sheets that I don't want it to consider
Can you please explain in detail what you mean by this using a few examples?
 

Tom98

New Member
Joined
Oct 26, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Can you please explain in detail what you mean by this using a few examples?
So currently I have:

|Front Cover | User guide | References | Button Sheet | Test_1 | Test_2 | Test_3 | Test_5 |

The solution you kindly provided produces:

|Front Cover | User guide | References | Button Sheet | Test_5 | Test_6 | Test_7 | Test_8 |

The solution I would ideally obtain:

|Front Cover | User guide | References | Button Sheet | Test_1 | Test_2 | Test_3 | Test_4 |

Is there a way of saying For each Worksheets in ThisWorkbook.Worksheets, if Sheets.Name<>"Test_*" then skip to next sheet? I don't know if this would work and don't know how to write it in vba exactly.

Cheers
 

Tom98

New Member
Joined
Oct 26, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
So currently I have:

|Front Cover | User guide | References | Button Sheet | Test_1 | Test_2 | Test_3 | Test_5 |

The solution you kindly provided produces:

|Front Cover | User guide | References | Button Sheet | Test_5 | Test_6 | Test_7 | Test_8 |

The solution I would ideally obtain:

|Front Cover | User guide | References | Button Sheet | Test_1 | Test_2 | Test_3 | Test_4 |

Is there a way of saying For each Worksheets in ThisWorkbook.Worksheets, if Sheets.Name<>"Test_*" then skip to next sheet? I don't know if this would work and don't know how to write it in vba exactly.

Cheers
I believe that changing it to
Sub ReNameSheets()
Application.ScreenUpdating = False
Dim x As Long
For x = ActiveSheet.Index + 1 To Sheets.Count
Sheets(x).Name = "Test_" & x - 4
Next x
Application.ScreenUpdating = True
End Sub

does the job.

Thanks for all your help :)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,924
Messages
5,639,025
Members
417,064
Latest member
oryngirl

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
Top