Selecting a dynamic range

nickos

New Member
Joined
Aug 30, 2006
Messages
13
Hi,

I want to create a loop that processes a number of non contiguous ranges. Here's the catch: each range has a different number of rows, so I want to select from the first row to the last row for each range.

Does anyone know code to achieve this action?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Can you provide some more details about what you are trying to do exactly. What ranges, what do you want to do with them, generally it's not necessary to select ranges in order to work with them.
 

nickos

New Member
Joined
Aug 30, 2006
Messages
13
Good call, HotPepper!

Yes, I only just realised this fact.

Imagine this scenario:

xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx


xxxxxxxxxxxx
xxxxxxxxxxxx


xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx

Imagine for a second that these are non-contiguous ranges. I wanted to create a For each ..next loop that would check through each of these and perform some further tests.

As you can see, the number of rows in each range is always different, so I don't know how to manipulate them. The number of rows and ranges differ from one worksheet to the next.

I want to be able to loop through them by checking whether some aspect of that range satisfies a particular condition. If it does, I want to test if further. If it doesn't, then move to the next range and check again.

I want to do this until I have tested all these ranges on the worksheet.

Does this make sense?

Nickos.
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Here's a very simple example checking varaible rows in columns A:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> foo()
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Range([A1], Cells(Rows.Count, "A").End(xlUp))
            <SPAN style="color:#00007F">If</SPAN> c = 1 <SPAN style="color:#00007F">Then</SPAN> c.Offset(, 1) = "YES"
        <SPAN style="color:#00007F">Next</SPAN> c
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,

Smitty
 

nickos

New Member
Joined
Aug 30, 2006
Messages
13
Thanks pennysaver.

Please forgive my ignorance, but I am still quite new at VBA and understanding what each line of code means.

Why do we test if the range c = 1? what's it's significance?

Thanks again,

Nickos.
 

Forum statistics

Threads
1,136,607
Messages
5,676,778
Members
419,651
Latest member
alexanderguhr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top