Setting a range using sheets and cells

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
Good morning,

I'm stumped what I'm doing wrong.

I know the following work and give the same result.
VBA Code:
Set rng1 = Range("A5:A11")
Set rng2 = Range(Cells(5, 1), Cells(11, 1))

The problem I'm having is when I add in the name of a worksheet.
VBA Code:
Set rng1 = Sheets("Continuous Learning Details").Range("A5:A11") 'This works perfectly
Set rng2 = Sheets("Continuous Learning Details").Range(Cells(5, 1), Cells(11, 1)) 'This gives me an error code 1004, Application-defined or object-defined error
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You need to qualify all ranges (including the Cells) with the worksheet like
VBA Code:
Set rng2 = Sheets("Continuous Learning Details").Range(Sheets("Continuous Learning Details").Cells(5, 1), Sheets("Continuous Learning Details").Cells(11, 1))
or shorter
VBA Code:
With Sheets("Continuous Learning Details")
   Set rng2 = .Range(.Cells(5, 1), .Cells(11, 1))
End With
 
Upvote 0
Solution
You need to qualify all ranges (including the Cells) with the worksheet like
VBA Code:
Set rng2 = Sheets("Continuous Learning Details").Range(Sheets("Continuous Learning Details").Cells(5, 1), Sheets("Continuous Learning Details").Cells(11, 1))
or shorter
VBA Code:
With Sheets("Continuous Learning Details")
   Set rng2 = .Range(.Cells(5, 1), .Cells(11, 1))
End With
That makes sense, thank you again. You're always so helpful.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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