Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Range until last value of a column

Hi,

I have a sheet and I want to loop through the values of column B from B2
to B10, so I used this code:

For Each c In Worksheets("TestSheet").Range("B2:B10")
....
Next c

The above code worked but I don't want B10 to be fixed but rather
variable i.e I want to select the range from B2 to last value in column
B, so I tried this:

For Each c Worksheets("TestSheet").Range("B2", Range("B2").End(xlDown))
....
Next c

However, the above didn't work, nothing happens when the code runs. So
my question is: how can I make the range be from B2 to the last value in
column B?

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Range until last value of a column

I like the with/end with to save typing:

dim myRng as range
dim c as range

with worksheets("testsheet")
set myrng = .range("b2",.cells(.rows.count,"B").end(xlup))
end with

for each c in myrng.cells
'...
next c


Scott Steiner wrote:

Hi,

I have a sheet and I want to loop through the values of column B from B2
to B10, so I used this code:

For Each c In Worksheets("TestSheet").Range("B2:B10")
...
Next c

The above code worked but I don't want B10 to be fixed but rather
variable i.e I want to select the range from B2 to last value in column
B, so I tried this:

For Each c Worksheets("TestSheet").Range("B2", Range("B2").End(xlDown))
...
Next c

However, the above didn't work, nothing happens when the code runs. So
my question is: how can I make the range be from B2 to the last value in
column B?

Thanks.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Range until last value of a column

That should work, unless B3 is empty, but others below aren't.

Try

For Each c In Worksheets("TestSheet").Range("B2",
Cells(Rows.Count,"B").End(xlUp).Row)


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Scott Steiner" wrote in message
...
Hi,

I have a sheet and I want to loop through the values of column B from B2
to B10, so I used this code:

For Each c In Worksheets("TestSheet").Range("B2:B10")
...
Next c

The above code worked but I don't want B10 to be fixed but rather
variable i.e I want to select the range from B2 to last value in column
B, so I tried this:

For Each c Worksheets("TestSheet").Range("B2", Range("B2").End(xlDown))
...
Next c

However, the above didn't work, nothing happens when the code runs. So
my question is: how can I make the range be from B2 to the last value in
column B?

Thanks.



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
AVERAGE a range in a column if another column's range equals a val bob$ Excel Discussion (Misc queries) 3 February 24th 09 07:42 AM
formula to sort a range so that it matches the exact rows of a column that is outside that range? steveo Excel Discussion (Misc queries) 1 June 18th 06 02:05 AM
How to count dates within a certain range in a column with mutiple date range entries Krisjhn Excel Worksheet Functions 2 September 1st 05 01:59 PM
CountIf first column range = "Word" and second column range <> 0 TinaMo Excel Worksheet Functions 3 June 3rd 05 10:56 PM
Transfering VBA Array Column Range to Excel Column Range ExcelMonkey[_11_] Excel Programming 5 January 22nd 04 05:57 PM


All times are GMT +1. The time now is 08:23 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"