0

My goal is to rename many worksheets from a worksheet that contains stock market codes. To do this I have the codes in a worksheet called Update. The codes are from A2 to A10. I have set up a For loop to goto the next activeworksheet and as the range value is increased, the worksheet gets renamed to the new cell value in the Update WSheet

The problem I have is that I want the Range value to increase by 1 which will select the next name for the worksheet. I have tried adding 1 to the Range value but did not work

Sub changeWSnames()
    Dim sheetname As Worksheet
    Dim r As Integer

    For r = 1 To 10
        ActiveWorkbook.Worksheets(r).Activate
        Set sheetname = ActiveWorkbook.ActiveSheet
        sheetname.Name = Worksheets("Update").Range("a2").Value 
    Next r

    r = r + 1
End Sub

What I need is too workout is how to increment the .Range("a2").Value , i.e. to increase by 1, example it becomes Range("a3").Value etc etc.

1
  • 1
    Don't use .Activate instead reference your sheet directly: Set sheetname = ActiveWorkbook.Worksheets(r) and you don't need the r = r + 1 line.
    – Pᴇʜ
    Commented Mar 25, 2019 at 12:35

1 Answer 1

2

Replace:

sheetname.Name = Worksheets("Update").Range("a2").Value 

with:

sheetname.Name = Worksheets("Update").Range("a" & (r+1)).Value 

So the first time through the loop we use a2, the next time we use a3, etc.

3
  • Would it not be better just to start the FOR loop at 2? That way you can just use r rather than r+1
    – Zac
    Commented Mar 25, 2019 at 13:30
  • @Zac It would be better because you would be avoiding doing unnecessary math in the loop. Commented Mar 25, 2019 at 13:35
  • @Zac You can't just start the loop at 2 because you would bypass the first worksheet.
    – GMalc
    Commented Mar 25, 2019 at 14:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.