0

I'm trying to name intervals in every worksheet according to the content of a specific cell in each worksheet. I've looked up many ways of naming the ranges, whoever none uses the content of a cell as a name. This is what I have (but it doesn't work):

Sub NameRanges()

Dim WS_Count As Integer
Dim I As Integer
Dim r As String

     WS_Count = ActiveWorkbook.Worksheets.Count


     For I = 5 To WS_Count

        r = Worksheets(I).Range("A2")
        Names.Add Name:="r", RefersTo:=Worksheets(I).Range("B6:B10000")


     Next I
End Sub 
1
  • 1
    Names.Add Name:=r? Variables don't belong inside quotes. Commented Jul 9, 2020 at 21:40

1 Answer 1

1

To name a sheet simply use its .Name property like this:

Sub NameSheets()
    Dim i As Integer
    With ActiveWorkbook
        For i = 5 To .Sheets.Count
            .Sheets(i).Name = .Sheets(i).Range("A2").Value
        Next i
    End with
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @SuperSymmetry, however it's still not working. I get a message saying that there's a syntax error regarding this part: Worksheets(i).Range("B6:B10000").Name = Worksheets(i).Range("A2").Value
Most likely because you don't have valid names in A2 of these worksheet. When it gives you the error, what name are you trying to give this range?
I'm actually trying to name 100 different sheets in the same workbook, each with a different name (in cell A2 of each sheet).
Why would you then say "I'm trying to name intervals in every worksheet". And why would you name (or use) a prcedure NameRanges. and make the name ReferTo Range("B6:B10000"). And why would you say in the question "I've looked up many ways of naming the ranges". Please make sure you understand what you're saying and write what you mean, otherwise, you're wasting the time of people that are trying to help you. Anyway, I updated my answer
First of all, thank you for your help, I really appreciate that! I misspoke on my latest comment, sorry about that. I have to name the ranges "B6:B10000" in every sheet according to the name in cell "A2" of that specific sheet. So it's the ranges I have to name, not the sheets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.