VBA: Reason for Range Variable not working

AD_Taylor

Well-known Member
Joined
May 19, 2011
Messages
687
Hi,

So I've declared and set a Range variable, lets call it checkRng for now.
I've also set up another Range variable lets say currCell.

So:
Code:
Dim checkRng As Range
Dim currCell As Range

Set checkRng = Range("A1:A245")

Now when I try and start a loop so that I am checking every cell in checkRng individually, I always thought I could do this:

Code:
For each currCell In checkRng

'Do some stuff here with currCell.Value etc

Next currCell

However, sometimes this works and sometimes it does not.
I have 2 macros (can post the code if needed) where this code works in one and does not work in the other.

In the one that it doesn't work in if I type ?currCell.Address in the Immediate window while debugging I can see that currCell has strangely taken the entire range from checkRng, so in this case currCell = A1:A245.

I've had to resort to counting rows and columns and looping based on these numbers using Cells(rowNum, colNum).Value.

Any ideas as to why this happens?

Thanks,
Adam
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
It is safer to specify what you are looping through:
Rich (BB code):
For each currCell In checkRng.Cells

'Do some stuff here with currCell.Value etc

Next currCell
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,513
Members
452,921
Latest member
BBQKING

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