PC Review


Reply
Thread Tools Rate Thread

Creating a name with VBA

 
 
=?Utf-8?B?TmV2aWxsZVQ=?=
Guest
Posts: n/a
 
      30th Jan 2007
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

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      30th Jan 2007
How about:

with activesheet
.range(.range(strstart), .range(strend)).name = "ResourceList"
end with



NevilleT wrote:
>
> 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


--

Dave Peterson
 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      30th Jan 2007
try this. Did you mean workbook or worksheet?

Sub makename1()
Dim strStart As String
Dim strEnd As String
Dim strAddress As String
strStart = "$A$1"
strEnd = "$A$3"
strAddress = ActiveSheet.Name & "!" & strStart & ":" & strEnd
range(strAddress).Name = "RL3"
End Sub

--
Don Guillett
SalesAid Software
(E-Mail Removed)
"NevilleT" <(E-Mail Removed)> wrote in message
news:04FE23F3-4969-48EF-B52E-(E-Mail Removed)...
>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
>



 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      30th Jan 2007
If you are using direct cell references, this one line will do everything
your code is doing. Your code is more usefull when dealing with variable
ranges (with some additional code added).

Range("A1:A3").Name = "ResourceList"

or


ActiveWorkbook.Names.Add Name:="ResourceList", RefersToR1C1:= _
"=Sheet1!R1C1:R3C1"


Alan


"The only dumb question is a question left unasked."


"NevilleT" <(E-Mail Removed)> wrote in message
news:04FE23F3-4969-48EF-B52E-(E-Mail Removed)...
>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
>



 
Reply With Quote
 
=?Utf-8?B?TmV2aWxsZVQ=?=
Guest
Posts: n/a
 
      31st Jan 2007
Thanks to you all for such a quick response. Actually the response from Don
and Alan both do the trick. I should have been using worksheet rather than
workbook but I had taken what was a complex part of another spreadsheet and
tried to simplify it into a single sheet with one function. I am using
variable addresses for the real application.

Thanks again guys.

"Alan" wrote:

> If you are using direct cell references, this one line will do everything
> your code is doing. Your code is more usefull when dealing with variable
> ranges (with some additional code added).
>
> Range("A1:A3").Name = "ResourceList"
>
> or
>
>
> ActiveWorkbook.Names.Add Name:="ResourceList", RefersToR1C1:= _
> "=Sheet1!R1C1:R3C1"
>
>
> Alan
>
>
> "The only dumb question is a question left unasked."
>
>
> "NevilleT" <(E-Mail Removed)> wrote in message
> news:04FE23F3-4969-48EF-B52E-(E-Mail Removed)...
> >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
> >

>
>
>

 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      31st Jan 2007
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" <(E-Mail Removed)> wrote in message
news:04FE23F3-4969-48EF-B52E-(E-Mail Removed)...
>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
>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      31st Jan 2007
If you're saying you wanted a worksheet level name, you could also use:

with activesheet
.range(.range(strstart), .range(strend)).name _
= "'" & .name & "'!ResourceList"
end with



NevilleT wrote:
>
> Thanks to you all for such a quick response. Actually the response from Don
> and Alan both do the trick. I should have been using worksheet rather than
> workbook but I had taken what was a complex part of another spreadsheet and
> tried to simplify it into a single sheet with one function. I am using
> variable addresses for the real application.
>
> Thanks again guys.
>
> "Alan" wrote:
>
> > If you are using direct cell references, this one line will do everything
> > your code is doing. Your code is more usefull when dealing with variable
> > ranges (with some additional code added).
> >
> > Range("A1:A3").Name = "ResourceList"
> >
> > or
> >
> >
> > ActiveWorkbook.Names.Add Name:="ResourceList", RefersToR1C1:= _
> > "=Sheet1!R1C1:R3C1"
> >
> >
> > Alan
> >
> >
> > "The only dumb question is a question left unasked."
> >
> >
> > "NevilleT" <(E-Mail Removed)> wrote in message
> > news:04FE23F3-4969-48EF-B52E-(E-Mail Removed)...
> > >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
> > >

> >
> >
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with creating query or creating report Need Help Microsoft Access Database Table Design 0 29th Apr 2008 02:22 AM
Creating VBA Functions When Creating Spreadsheet Via VBA? PeteCresswell Microsoft Excel Programming 6 18th Jun 2007 12:38 PM
creating PDF from msword - I get error while creating bookmarks =?Utf-8?B?bWFyay1uZWVkcy1oZWxw?= Microsoft Word Document Management 1 1st Nov 2005 05:48 AM
Auto creating text fields when creating new slide. =?Utf-8?B?amFtZXM=?= Microsoft Powerpoint 1 16th Jun 2005 04:02 PM
Walkthrough: Creating a Dist. App. - problems creating project =?Utf-8?B?R2FyeURvdE5ldA==?= Microsoft Dot NET 1 12th Aug 2004 03:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:22 AM.