Creating a name with VBA
You have received some workarounds, but you have two problems in your
original code:
1) you are passing an A1 Style reference and using the R1C1 property
2) you omitted the equal sign in the argument to RefersTo.
The below works fine:
Sub test()
Dim strStart As String
Dim strEnd As String
Dim strAddress As String
strStart = "$A$1"
strEnd = "$A$3"
strAddress = ActiveWorkbook.Name & "!" & strStart & ":" & strEnd
ActiveWorkbook.Names.Add Name:="ResourceList", RefersTo:= "=" &
strAddress
End Sub
--
Regards,
Tom Ogilvy
"NevilleT" wrote in message
...
I have an obscure problem. I have simplified it down to the code below. I
am trying to create a named range with VBA but the range name has inverted
commas around it so it will not work. For example, the code below creates
a
named range for ResourceList of:
="Sheet2!$A$1:$A$3" rather than =Sheet2!$A$1:$A$3 (with no inverted
commas)
"Sub test()
Dim strStart As String
Dim strEnd As String
Dim strAddress As String
strStart = "$A$1"
strEnd = "$A$3"
strAddress = ActiveWorkbook.Name & "!" & strStart & ":" & strEnd
ActiveWorkbook.Names.Add Name:="ResourceList", RefersToR1C1:=strAddress
End Sub
|