Referencing a range in another worksheet.

JWyath

New Member
Joined
Jul 22, 2011
Messages
2
I want to loop over the values in a range on my ActiveWorksheet (Sheet1) and compare them to values in a range on another worksheet (Sheet2).

Here's my original code:

Code:
Sub LoopOverRange()
    Dim fromColumn As range
    Dim toColumn As range
    
    Set fromColumn = Sheet1.range("C2", range("C20000").End(xlUp))
    Set toColumn = Sheet2.range("A2", range("A20000").End(xlUp))

    For Each c In fromColumn.Cells
        If c <> "null" Then
            For Each d In toColumn.Cells
                If c.Value = d.Value Then
                    Call doSomething(c, d)
                End If
            Next
        End If
    Next
End Sub
This gives me "runtime error 1004"

Here's two test bits of code :

Code:
Sub testRange()
    Dim myRange As range
    Set myRange = Sheet1.range("C2", range("C20000").End(xlUp))
End Sub
This works.

Code:
Sub testRange()
    Dim myRange As range
    Set myRange = Sheet2.range("C2", range("C20000").End(xlUp))
End Sub
This gives me "runtime error 1004"

How can I achieve my goal and why does my first test code work while my second test code fails?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Welcome to MrExcel.

You need to qualify both instances of the Range property:

Rich (BB code):
Sub testRange()
    Dim myRange As range
    Set myRange = Sheet1.Range("C2", Sheet1.Range("C20000").End(xlUp))
End Sub
 
Upvote 0
Thanks for the prompt reply, Andrew.

That worked, although I still think it's peculiar that the first of my test cases works while the second doesn't, even if I activate Sheet2 beforehand.
 
Upvote 0
Excel uses the ActiveSheet if the property is unqualified, except when the code is in the module for a Worksheet when that sheet is used.
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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