Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Working with one Column in a Named Range

This must be simple...

I want to iterate across all cells in the second column of a named range
("Data_Range"). The following For-Each does not work...

For Each cell In Range("Data_Range").Columns(2)
Debug.Print cell.Address
Next cell

For each iteration, this prints the address of the entire named range, not
the address of each cell in the range. Why is that?

The following works using the single-parameter version of Cells()...
For i = 1 To Range("Data_Range").Columns(2).Cells.Count
Debug.Print Range("Data_Range").Columns(2).Cells(i).Value
Next I


Steve

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Working with one Column in a Named Range

did you dim cell as Range?

Try:
Dim c as Range
For Each c In Range("Data_Range").Columns(2).Cells
Debug.Print c.Address
Next cell


--
Tim Williams
Palo Alto, CA


"Steve Drenker" wrote in message ...
This must be simple...

I want to iterate across all cells in the second column of a named range
("Data_Range"). The following For-Each does not work...

For Each cell In Range("Data_Range").Columns(2)
Debug.Print cell.Address
Next cell

For each iteration, this prints the address of the entire named range, not
the address of each cell in the range. Why is that?

The following works using the single-parameter version of Cells()...
For i = 1 To Range("Data_Range").Columns(2).Cells.Count
Debug.Print Range("Data_Range").Columns(2).Cells(i).Value
Next I


Steve



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Working with one Column in a Named Range

Steve,

From XL 2003 help:

Columns returns a Range object that represents the columns in the specified
range.

So it returns the address of the whole range represented by the column, just
as "Debug.Print Columns(2).Address" returns:

$B:$B

Another way to get what you want is:

For Each cell In Intersect(Range("Data_Range"),
Range("Data_Range").Columns(2))
Debug.Print cell.Address
Next cell

hth,

Doug

"Steve Drenker" wrote in message
...
This must be simple...

I want to iterate across all cells in the second column of a named range
("Data_Range"). The following For-Each does not work...

For Each cell In Range("Data_Range").Columns(2)
Debug.Print cell.Address
Next cell

For each iteration, this prints the address of the entire named range, not
the address of each cell in the range. Why is that?

The following works using the single-parameter version of Cells()...
For i = 1 To Range("Data_Range").Columns(2).Cells.Count
Debug.Print Range("Data_Range").Columns(2).Cells(i).Value
Next I


Steve



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Working with one Column in a Named Range

Sometimes, just being explicit will help:

Dim Cell as Range
For Each Cell In Range("Data_Range").Columns(2).Cells



Steve Drenker wrote:

This must be simple...

I want to iterate across all cells in the second column of a named range
("Data_Range"). The following For-Each does not work...

For Each cell In Range("Data_Range").Columns(2)
Debug.Print cell.Address
Next cell

For each iteration, this prints the address of the entire named range, not
the address of each cell in the range. Why is that?

The following works using the single-parameter version of Cells()...
For i = 1 To Range("Data_Range").Columns(2).Cells.Count
Debug.Print Range("Data_Range").Columns(2).Cells(i).Value
Next I

Steve


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Working with one Column in a Named Range

Dave,

That's one of the syntactical VBA patterns I have trouble remembering.
E.g., I try

"For each ws in Thisworkbook" or "For each ctl in UserForm1"

more often than I care to admit - except I guess I just did (admit it that
is).

Have a good weekend!

Doug


"Dave Peterson" wrote in message
...
Sometimes, just being explicit will help:

Dim Cell as Range
For Each Cell In Range("Data_Range").Columns(2).Cells



Steve Drenker wrote:

This must be simple...

I want to iterate across all cells in the second column of a named range
("Data_Range"). The following For-Each does not work...

For Each cell In Range("Data_Range").Columns(2)
Debug.Print cell.Address
Next cell

For each iteration, this prints the address of the entire named range,
not
the address of each cell in the range. Why is that?

The following works using the single-parameter version of Cells()...
For i = 1 To Range("Data_Range").Columns(2).Cells.Count
Debug.Print Range("Data_Range").Columns(2).Cells(i).Value
Next I

Steve


--

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
Named Range Links not working Fleone Links and Linking in Excel 1 November 12th 08 07:49 AM
My Dynamic Named Range isn't working Carrie_Loos via OfficeKB.com Excel Worksheet Functions 11 February 22nd 08 10:09 PM
Formula not working when letter A is used in a named range jimar Excel Discussion (Misc queries) 7 July 6th 06 11:43 AM
working with a named range Gixxer_J_97[_2_] Excel Programming 2 June 1st 05 07:44 PM
Create named range for each column not working? Ed Excel Programming 3 January 12th 05 05:57 PM


All times are GMT +1. The time now is 06:47 AM.

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"