Question

I have to rename my selected sheets (tabs of sheets) continuously in order to keep them updated with new additions i.e. sorting them based on new names, numbers or other criteria.

I have made a table of all the names in one particular sheet and after processing that table (changing names, numbers etc) I want to use it to rename the currently selected sheets only.

e.g. I select ANY 5 sheets and I want a code that will rename ONLY those 5 sheets based on FIRST FIVE values from the table in particular SHEET.

I did little bit of scratch work as below but it's not leading me anywhere.

Sub Sheets_Naming_current()

Dim ws As Worksheet, wb As Workbook
Dim nmRange As Range
Dim count As Integer

Set wb = ActiveWorkbook
Set nmRange = wb.Sheets("Totals").Range("D2:D25")
count = 0
On Error Resume Next
For Each ws In ActiveWindow.SelectedSheets
 count = count + 1     
      ws.Name = nmRange.Offset(i, 0).Value
Next ws


End Sub

Thanks in advance!

Was it helpful?

Solution

You need to made some changes inside the loop.

First- your i variable returns zero all the time, you rather need to use count variable when offsetting.

Second- instead of offset I would suggest to use Cells() reference like presented below.

Proposed loop could be as follows:

'...your code here
For Each ws In ActiveWindow.SelectedSheets

      count = count + 1
      ws.Name = nmRange.Cells(count).Value

Next ws
'...your code here

OTHER TIPS

Try below code :

Sub Sheets_Naming_current()

    Dim ws As Worksheet, wb As Workbook
    Dim count As Integer

    Set wb = ActiveWorkbook
    count = 2
    On Error Resume Next

    For Each ws In ActiveWindow.SelectedSheets
        With Sheets("Totals")
            If ws.Name <> "Totals" And .Cells(count, 4) <> "" Then
                ws.Name = .Cells(count, 4)
                count = count + 1
            End If
        End With
    Next ws

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top