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
 

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Barrie Davidson

MrExcel MVP
Joined
Feb 10, 2002
Messages
2,330
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,
 

TommyGun

MrExcel MVP
Joined
Dec 9, 2002
Messages
4,202
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>
 

DRJ

MrExcel MVP
Joined
Feb 17, 2002
Messages
3,853
Or

For Each cell In Range("A1:D12,G3:J16,A18:C30")
 

Watch MrExcel Video

Forum statistics

Threads
1,123,467
Messages
5,601,827
Members
414,479
Latest member
Beau the dog

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