Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Worksheet Name RefersTo

I'm trying to select a range of cells and name them based on the name
of the worksheet

Sub NameBlock()
myLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
myLastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
ThisWorkbook.Names.Add Name:=Worksheets(1).Name + "_total", _
RefersTo:="=$A$2:"&myLastRow, &myLastCol, Visible:=True
End Sub

Something is wrong
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Worksheet Name RefersTo

Try this instead. Your columns are numeric and should be a letter.

Sub NameBlock()
myLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
myLastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
Set MyRange = Range(Range("A1"),cells(myLastRow,myLastCol))

ThisWorkbook.Names.Add Name:=Worksheets(1).Name + "_total", _
RefersTo:="=" & MyRange.Address, Visible:=True
End Sub


"caveman.savant" wrote:

I'm trying to select a range of cells and name them based on the name
of the worksheet

Sub NameBlock()
myLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
myLastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
ThisWorkbook.Names.Add Name:=Worksheets(1).Name + "_total", _
RefersTo:="=$A$2:"&myLastRow, &myLastCol, Visible:=True
End Sub

Something is wrong

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Worksheet Name RefersTo

The problem is the code you are using, the refersto has invalid content, as
the other user also said, but if you resolve your string, it would become:
"=$A$2:"&myLastRow, &myLastCol
if last row is 1000 and last column is 10
then
=$A$2:1000, 10 'which makes no sense! you should be getting an error in this
line of code, right ?

You should use the suggested above, or:
"=$A$2:" & Worksheets(1).Cells(myLastRow, myLastCol).address

Makes sense?

:)

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Worksheet Name RefersTo

The reference is wrong. Refer the range as

Dim rngTemp as Range

Set rngTemp = Range(Cells(2, 1), Cells(myLastRow, myLastCol))

and set the reference as RefersTo:= "=" & rngTemp.Address

If this post helps click Yes
---------------
Jacob Skaria


"caveman.savant" wrote:

I'm trying to select a range of cells and name them based on the name
of the worksheet

Sub NameBlock()
myLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
myLastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
ThisWorkbook.Names.Add Name:=Worksheets(1).Name + "_total", _
RefersTo:="=$A$2:"&myLastRow, &myLastCol, Visible:=True
End Sub

Something is wrong

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Worksheet Name RefersTo

I like this syntax:

Sub NameBlock2()
Dim myLastRow as long
dim myLastCol as long

with activesheet
myLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
myLastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
.range("A2",.cells(mylastrow,mylastcol)).name = .name & "_total"
End with
End Sub



worksheets(1) is the activesheet, right?

And when you use it in a name, it's valid, right?

You may want to use a worksheet level name and not have to worry about the name
of the sheet creating a valid name:

Sub NameBlock3()
Dim myLastRow as long
dim myLastCol as long

with activesheet
myLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
myLastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
.range("A2",.cells(mylastrow,mylastcol)).name = "'" & .name & "'!_total"
End with
End Sub

ps. VBA is very forgiving--but not always.

You may want to start using & to concatenate strings and + to add values.



"caveman.savant" wrote:

I'm trying to select a range of cells and name them based on the name
of the worksheet

Sub NameBlock()
myLastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
myLastCol = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
ThisWorkbook.Names.Add Name:=Worksheets(1).Name + "_total", _
RefersTo:="=$A$2:"&myLastRow, &myLastCol, Visible:=True
End Sub

Something is wrong


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
issue with RefersTo in non-EN languages scoober Excel Programming 0 August 28th 07 02:32 PM
refersTo property Dev Excel Programming 0 May 18th 06 09:21 PM
Help building string for Names.Add RefersTo, pls? Ed Excel Programming 10 April 7th 05 05:00 PM
Wierd named range RefersTo value chris Excel Programming 1 May 21st 04 04:40 PM
Wierd named range RefersTo value Bob Phillips[_6_] Excel Programming 0 May 21st 04 04:32 PM


All times are GMT +1. The time now is 03:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"