VB Code to Name Only Selected Sheets based on data in worksheet cells

vb dummy

New Member
Joined
Mar 22, 2018
Messages
2
I am trying to write VB code to pick selected tabs, reference cell A1 in that tab and rename the tab.

I've literally only started using VB a couple of hours ago but I'm a long time excel user.

I read a couple of threads on this site that were answers to similar questions I was asking but not exactly and here is what I've cobbled together:

Sub Rename_Select_Sheets()
sheetlist = Array(Sheet1, Sheet2, Sheet4)
For i = LBound(sheetlist) To UBound(sheetlist)
Worksheets(sheetlist(i)).Activate
Worksheet.Name = "CPX - " + Range("a1")
Next
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the Forum!

It's not clear from your post whether your sheets are named "Sheet1", "Sheet2" and "Sheet4", or whether these are the code names.

Code:
'If your sheets are called "Sheet1" etc
Sub Rename_Select_Sheets()
    
    Set sheetlist = Worksheets(Array("Sheet1", "Sheet2", "Sheet4"))
    
    For Each sh In sheetlist
        sh.Name = "CPX - " & sh.Range("A1").Value
    Next sh

End Sub

'OR, if Sheet1 etc are the codenames
Sub Rename_Select_Sheets()
    
    sheetlist = Array(Sheet1, Sheet2, Sheet4)
    
    For Each sh In sheetlist
        sh.Name = "CPX - " & sh.Range("A1").Value
    Next sh

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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