For each Cell in More than one range. Possible

ajmckenna

Board Regular
Joined
Oct 7, 2002
Messages
145
I want the code to look in multiple ranges, Instead of recreating the code for each range


Example

For each Cell in ..."Myrange1" and Myrange2" and"Myrange3" etc

If Cell <>"" then

...code

End if

next

End sub

Any ideas? Thanks!

AJ
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Code:
Dim CheckRange As Range

Set CheckRange = Union(Range("MyRange1"), Range("MyRange2"), Range("MyRange3"))
For Each C In CheckRange
    If Cell <> "" Then
        'your code
    End If
Next

Should do the trick.

Regards,
 
Upvote 0
Create a new range using the Union of your individual ranges...

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ForEachInUnion()
    <SPAN style="color:#00007F">Dim</SPAN> unionRange <SPAN style="color:#00007F">As</SPAN> Range
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range
    
    <SPAN style="color:#00007F">Set</SPAN> unionRange = Application.Union(Range("A1:B10"), Range("E1:F10"), Range("H1:I10"))
    
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> unionRange.Cells
        <SPAN style="color:#00007F">If</SPAN> c <> vbNullString <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#007F00">' code here</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> unionRange = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,215,877
Messages
6,127,494
Members
449,385
Latest member
KMGLarson

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
Back
Top