Set Range on worksheet

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
My workbook has two worksheets, Sheet1 and Sheet2.

If I put this code in a standard module, it's fine:

Code:
Sub SetRange()

    Dim rng As Range
    Set rng = Range(Sheet1.Cells(1, 1), Sheet1.Cells(100, 1))

End Sub


but why is it when I put it in Sheet2, it fails:

Code:
Private Sub Worksheet_Activate()

    Dim rng As Range
    Set rng = Range(Sheet1.Cells(1, 1), Sheet1.Cells(100,1))

End Sub

with this message:

Code:
Method 'Range' of object '_Worksheet' failed

Thanks


EDIT SORTED:

Need to fully qualify, so

Code:
Set rng = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(100,1))
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try:
Code:
Sub SetRange()
    Dim rng As Range
    Set rng = Range(Sheets(1).Cells(1, 1), Sheets(1).Cells(100, 1))
End Sub

Private Sub Worksheet_Activate()
    Dim rng As Range
    Set rng = Sheets(1).Range(Sheets(1).Cells(1, 1), Sheets(1).Cells(100, 1))
End Sub
 
Upvote 0
Hi,
It's because you refer to sheet1 within worksheet 2 module. The question is what range you want to set out of worksheet2 module. Is it a range in sheet2 or range in sheet1. Depending on your choice take below code 1 or 2 and paste in your sheet2 module.

Code to set range sheet1 out of sheet2 module:
Code:
Dim rng As Range
    Set rng = sheets(1).Range(Cells(1, 1).address, Cells(100, 1).address)

Code to set range sheet 2 out of sheet 2 module

Code:
Dim rng As Range
    Set rng = Range(Cells(1, 1),Cells(100, 1))
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,858
Members
449,194
Latest member
HellScout

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