Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / August 2007

Tip: Looking for answers? Try searching our database.

Need help with adding a range name

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 31 Aug 2007 03:12 GMT
I select a range by using the following code:

Worksheets(Sheet2Name).Cells(Sheet2FirstRowNum, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select

Is there a way I can "convert" the aforementioned selected range (which can
vary) into the required R1C1 format so that I end up with something like this:

ActiveWorkbook.Names.Add Name:="Database", RefersToR1C1:= _
"=Worksheets(Sheet2Name)!R2C1:R18C11"

Any help would be greatly appreciated.  Thanks.
Gary Keramidas - 31 Aug 2007 03:39 GMT
i hard coded the variable, but see if this will work:

Sub test()
Dim firstrownum As Long
firstrownum = 6
With Worksheets("Sheet2").Cells(firstrownum, 1)
ActiveWorkbook.Names.Add Name:="database", RefersTo:=Range(.Address, _
   .End(xlDown).End(xlToRight))
End With
End Sub

Signature

Gary

>I select a range by using the following code:
>
[quoted text clipped - 9 lines]
>
> Any help would be greatly appreciated.  Thanks.
Bob - 31 Aug 2007 04:22 GMT
Gary,
Your code worked perfectly!  Thanks a million!
Bob

> i hard coded the variable, but see if this will work:
>
[quoted text clipped - 20 lines]
> >
> > Any help would be greatly appreciated.  Thanks.
- - 31 Aug 2007 03:49 GMT
With Worksheets(Sheet2Name)
   .Range(.Cells(Sheet2FirstRowNum, 1),  _
   .Cells(Sheet2FirstRowNum, 1).End(xlDown).End(xlToRight)).Name =
"Database"
End With

>I select a range by using the following code:
>
[quoted text clipped - 11 lines]
>
> Any help would be greatly appreciated.  Thanks.
Bob - 31 Aug 2007 04:26 GMT
Thanks for the help.  Unfortunately, I received a compile error message
("Invalid or unqualified reference"), and ".Cells" in line 2 of your code was
highlighted.

> With Worksheets(Sheet2Name)
>     .Range(.Cells(Sheet2FirstRowNum, 1),  _
[quoted text clipped - 17 lines]
> >
> > Any help would be greatly appreciated.  Thanks.
- - 31 Aug 2007 04:54 GMT
Works for me perfectly. I created a sheet name of "Sheet1" and a FirstRowNum
of 5 and created a contiguous block of data.

Rolling back the clock a bit, does this help?

Worksheets(Sheet2Name).Cells(Sheet2FirstRowNum, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Name = "Database"

The last line was the difference I was trying to note, which was that
there's no need to use the Names.Add nonsense.

Good luck.

> Thanks for the help.  Unfortunately, I received a compile error message
> ("Invalid or unqualified reference"), and ".Cells" in line 2 of your code
[quoted text clipped - 23 lines]
>> >
>> > Any help would be greatly appreciated.  Thanks.
Bob - 31 Aug 2007 13:52 GMT
Thanks!  Your revised code block worked perfectly.
Thanks again.
Bob

> Works for me perfectly. I created a sheet name of "Sheet1" and a FirstRowNum
> of 5 and created a contiguous block of data.
[quoted text clipped - 38 lines]
> >> >
> >> > Any help would be greatly appreciated.  Thanks.
Dave Peterson - 31 Aug 2007 13:56 GMT
If there are no gaps in columns or rows, you could try this:

with worksheets(Sheet2Name)
  .cells(sheet2firstrownum,1).currentregion.name _
    = "'" & .name & "'!database"
end with

In any case, if you want to make the name local (a sheet level name), you'll
want to add something like:

 .name = "'" & worksheets(sheet2name).name & "'!database
or maybe just:
 .name = "'" & sheet2name & "'!database

if sheet2name is really the name of the sheet.

> I select a range by using the following code:
>
[quoted text clipped - 9 lines]
>
> Any help would be greatly appreciated.  Thanks.

Signature

Dave Peterson

- - 31 Aug 2007 15:43 GMT
Or the modified version to capture single spaces in the sheet:

.name = "'" & Replace(sheet2name,"'","''") & "'!database

> If there are no gaps in columns or rows, you could try this:
>
[quoted text clipped - 28 lines]
>>
>> Any help would be greatly appreciated.  Thanks.
Dave Peterson - 31 Aug 2007 17:39 GMT
That's a good way to fix the apostrophes in the worksheet name--not the spaces.

(But it's a good fix!)

> Or the modified version to capture single spaces in the sheet:
>
[quoted text clipped - 36 lines]
> >
> > Dave Peterson

Signature

Dave Peterson

- - 31 Aug 2007 17:43 GMT
Sorry, I meant to type single quotes.

> That's a good way to fix the apostrophes in the worksheet name--not the
> spaces.
[quoted text clipped - 43 lines]
>> >
>> > Dave Peterson
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.