Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default dynamically referencing ranges with variables

I'm hung up on what should be a simple problem:

I have a spreadsheet that adds data to columns from column G on
depending on the number of elements I have in an array (each array
element heads up a column).

Once the code is complete, I need to format the sheet and this is
where I get into problems.

For example, lets say the array had five elements in it, so my code
creates data in columns G through K. if I want to set the column width
of columns G-K to 5 I can either code it statically
[Columns("G:K").ColumnWidth = 5] which eliminates any ability to
dynamically react to my array size or I can write a quick loop:

With Range("A1").CurrentRegion
col = .Columns.Count
End With

for i = 7 to col
Columns(i).ColumnWidth = 5
next i

But is there a way to name my range dynamically with a single
statement?

Art
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default dynamically referencing ranges with variables

This will give you the last column with an entry in Row 1 of the activesheet.
It may also execute faster htan what you have.

Dim aWS as excel.worksheet
Dim lCol as long
Dim myRange as excel.range

set aWS = activesheet

lCol = aws.cells(1,aws.columns.count).end(xltoleft).colum n

Set myrange = aws.cells(1,7).resize(aws.rows.count,lcol - 7 + 1)
myRange.columnwidth = 5


--
HTH,

Barb Reinhardt



"c1802362" wrote:

I'm hung up on what should be a simple problem:

I have a spreadsheet that adds data to columns from column G on
depending on the number of elements I have in an array (each array
element heads up a column).

Once the code is complete, I need to format the sheet and this is
where I get into problems.

For example, lets say the array had five elements in it, so my code
creates data in columns G through K. if I want to set the column width
of columns G-K to 5 I can either code it statically
[Columns("G:K").ColumnWidth = 5] which eliminates any ability to
dynamically react to my array size or I can write a quick loop:

With Range("A1").CurrentRegion
col = .Columns.Count
End With

for i = 7 to col
Columns(i).ColumnWidth = 5
next i

But is there a way to name my range dynamically with a single
statement?

Art
.

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
Dynamically declare and populate variables Gary@Dwight Excel Programming 6 December 5th 07 06:27 PM
Referencing a cell dynamically jliz2803 Excel Worksheet Functions 3 February 14th 06 09:40 PM
Dynamically create a formula in Sheet B referencing SheetA SHIPP Excel Programming 4 April 17th 05 04:11 PM
Referencing labels dynamically. liddlem[_3_] Excel Programming 2 January 16th 04 05:24 PM
Dynamically declaring variables with composite name Al[_10_] Excel Programming 2 October 16th 03 11:30 AM


All times are GMT +1. The time now is 08:41 AM.

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"