Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Named Range Links not working | Links and Linking in Excel | |||
My Dynamic Named Range isn't working | Excel Worksheet Functions | |||
Formula not working when letter A is used in a named range | Excel Discussion (Misc queries) | |||
working with a named range | Excel Programming | |||
Create named range for each column not working? | Excel Programming |