Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default VBA Excel how to assign name in variable range

I need to name a group of cells in vba. I need to add a few lines just to
name cells in my macro and refer to that range. The problem is the range (row
and Col) is variable. The macro I wrote do not work and keep the same range.
Here my needs:
1- Table always start at cell A4
2- Row and columns are variable and can be anything
3- I need to add this vba line inside a macro.
Here the line I did to name cells.
If I create a new file with more rows the macro keep the same range R20C11 and
name the cells. Macro do not go to the last cells. Why?
Thanks
==========
Range("A4").Select
Selection.SpecialCells(xlCellTypeLastCell).Select
ActiveWorkbook.Names.Add Name:="HvacTable", RefersToR1C1:= _
"=Sheet1!R4C1:R20C11"
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 772
Default VBA Excel how to assign name in variable range

You need to make the last row variable, try this
Sub main()
Dim lastCell As Integer
lastCell = Sheet1.Cells(Rows.Count, "a").End(xlUp).Row
Sheet1.Range("A4").Select
ActiveWorkbook.Names.Add Name:="HvacTable",
RefersToR1C1:="=Sheet1!R4C1:R" & lastCell & "C1"
End Sub
--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"Mouimet" wrote:

I need to name a group of cells in vba. I need to add a few lines just to
name cells in my macro and refer to that range. The problem is the range (row
and Col) is variable. The macro I wrote do not work and keep the same range.
Here my needs:
1- Table always start at cell A4
2- Row and columns are variable and can be anything
3- I need to add this vba line inside a macro.
Here the line I did to name cells.
If I create a new file with more rows the macro keep the same range R20C11 and
name the cells. Macro do not go to the last cells. Why?
Thanks
==========
Range("A4").Select
Selection.SpecialCells(xlCellTypeLastCell).Select
ActiveWorkbook.Names.Add Name:="HvacTable", RefersToR1C1:= _
"=Sheet1!R4C1:R20C11"

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default VBA Excel how to assign name in variable range

I tried both the macros (from John and Don) alone first, and the first macro
named only the first column. The second one do not work at all.
Maybe I didn't give you enough details or didn't explain well. Sorry.
Here
1- I use this in personnal macro because I use it with different files.
2- The macro I use right now insert columns, add formula, create Pivot Table
base on the table I'm trying to give a name. (The same macro do all of this).
Naming the table is part of the macro.

3- Everything work except fine when I try to named the table from cell
A4(always) to (variable columns and Rows)Example Col: J,K or L rows 10, 20,
98, etc.
The macro close without making a mistake, however the table is always a4 to
I20
The cell A4 will always be the first cell of the table. The column I will be
normally I all the time. The row can be anything 5 to 3000.

Why when I ask to go to the last cell the macro always stop to row 20 even
if I have 40 rows. I even tried to use the "record macro using the option
"relative References" and it doesn't change. If I use the CTRL-End (shortcut)
the curseur go to the last cell so it's not a problem with the sheets or the
data.
Can you help me on this. Thanks for the work you did already.


"John Bundy" wrote:

You need to make the last row variable, try this
Sub main()
Dim lastCell As Integer
lastCell = Sheet1.Cells(Rows.Count, "a").End(xlUp).Row
Sheet1.Range("A4").Select
ActiveWorkbook.Names.Add Name:="HvacTable",
RefersToR1C1:="=Sheet1!R4C1:R" & lastCell & "C1"
End Sub
--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"Mouimet" wrote:

I need to name a group of cells in vba. I need to add a few lines just to
name cells in my macro and refer to that range. The problem is the range (row
and Col) is variable. The macro I wrote do not work and keep the same range.
Here my needs:
1- Table always start at cell A4
2- Row and columns are variable and can be anything
3- I need to add this vba line inside a macro.
Here the line I did to name cells.
If I create a new file with more rows the macro keep the same range R20C11 and
name the cells. Macro do not go to the last cells. Why?
Thanks
==========
Range("A4").Select
Selection.SpecialCells(xlCellTypeLastCell).Select
ActiveWorkbook.Names.Add Name:="HvacTable", RefersToR1C1:= _
"=Sheet1!R4C1:R20C11"

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default VBA Excel how to assign name in variable range

I tried both the macros (from Don and John) and the first macro
named only the first column. The second one do not work at all sorry Don.
Maybe I didn't give you enough details or didn't explain well. Sorry.
Here
1- I use this in personnal macro because I use it with different files.
2- The macro I use right now insert columns, add formula, create Pivot Table
base on the table I'm trying to give a name. (The same macro do all of this).
Naming the table is part of the macro.

3- Everything work fine except when I try to named the table from cell
A4(always) to (variable columns and Rows)Example Col: J,K or L rows 10, 20,
98, etc.
The macro close without making a mistake, however the table is always a4 to
I20
The cell A4 will always be the first cell of the table. The column I will be
normally I all the time. The row can be anything 5 to 3000.

Why when I ask to go to the last cell the macro always stop to row 20 even
if I have 40 rows. I even tried to use the "record macro using the option
"relative References" and it doesn't change. If I use the CTRL-End (shortcut)
the curseur go to the last cell so it's not a problem with the sheets or the
data.
Can you help me on this. Thanks for the work you did already.



"Don Guillett" wrote:

Try this. Your defined name needs to be variable. Look in the help index for
OFFSET

Sub variablerange()
Sheets("sheet1").Select
ActiveWorkbook.Names.Add Name:="HvacTable", RefersTo:= _
"=offset($A$3,1,0,counta($r:$r)-3,11)"

' "=Sheet1!R4C1:R20C11"
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Mouimet" wrote in message
...
I need to name a group of cells in vba. I need to add a few lines just to
name cells in my macro and refer to that range. The problem is the range
(row
and Col) is variable. The macro I wrote do not work and keep the same
range.
Here my needs:
1- Table always start at cell A4
2- Row and columns are variable and can be anything
3- I need to add this vba line inside a macro.
Here the line I did to name cells.
If I create a new file with more rows the macro keep the same range R20C11
and
name the cells. Macro do not go to the last cells. Why?
Thanks
==========
Range("A4").Select
Selection.SpecialCells(xlCellTypeLastCell).Select
ActiveWorkbook.Names.Add Name:="HvacTable", RefersToR1C1:= _
"=Sheet1!R4C1:R20C11"



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
How can I make Excel read a value from a textfile and assign it to a variable in my VBA code?? haakon Excel Programming 2 February 28th 07 09:21 AM
range variable won't assign (chartobject.topleftcell property) Matthew Dodds Excel Programming 2 November 16th 05 02:25 PM
How to assign a variable in a range select Paul Excel Programming 5 June 3rd 05 11:50 PM
How can I assign a range starting cell based on a variable locati. feman007 Excel Discussion (Misc queries) 1 March 9th 05 11:41 PM
How can I assign a range starting cell based on a variable locati. feman007 Excel Worksheet Functions 3 March 9th 05 11:40 PM


All times are GMT +1. The time now is 05:17 PM.

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

About Us

"It's about Microsoft Excel"