2

I want to name a range in an excel sheet that is created dynamically. I tried following but it is not working.

With oWB
  For intCount = 1 To wbnum1
    .Worksheets.Add(After:=.Worksheets(intCount + 1)) 
    .Sheets(intCount + 1).Name = "Sheet" & intCount.ToString()
    .Sheets(intCount + 1).Cells(19, 2).value = "Sheet" & intCount.ToString
    .Sheets(intCount+1).Range("A1:Z1")
    .Range.Name = "Numbers"
  Next

what is wrong with this?

Thanks

1
  • Try: .Sheets(intCount+1).Range("A1:Z1").Name = "Numbers" Commented Nov 1, 2012 at 4:48

2 Answers 2

2

You aren't specifying a Range.

Presumably, you should merge your last and second to last line:

.Sheets(intCount+1).Range("A1:Z1").Name = "Numbers"

However, from experimentation it appears that assigning the name this way creates workbook level names resulting in only the last assignment being valid after you loop through your worksheets.

An alternate/safer way to do this is to add the name as a worksheet specific name via the worksheet's names collection:

.Sheets(intCount+1).Names.Add "Numbers", .Sheets(intCount+1).Range("A1:Z1")
Sign up to request clarification or add additional context in comments.

Comments

1
Dim uniqueName as String = "A_NamedRangeName_" & i
Dim rnArea as Range = .ActiveSheet.Range(leftCol & startRow, rightCol & endRow)
Name name = .ActiveBook.Names.Add(uniqueName, rnArea)

Name Range names cannot:
- start with digits
- include , : ; characters
- be duplicate names, each must have a unique name
- be a reserved excel function name

Comments

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.