Object defined error?

cheoksoon

Board Regular
Joined
Aug 25, 2016
Messages
56
Hi there,

I'm baffled by a seemingly trivial problem in VBA, maybe I didn't define something? I was hoping ya'll can help shed light on this. :)

Basically, this code works:
Code:
Dim roster As Worksheet
With ThisWorkbook
        Set roster = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        roster.Name = "Day shift"
End With
roster.Range("A1") = some value

But this code doesn't work?

Code:
Dim roster As Worksheet
With ThisWorkbook
        Set roster = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        roster.Name = "Day shift"
End With
roster.Range(Cells(1, 1), Cells(1, 2)) = some value

It says runtime error 1004.

Am I missing something?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You need to fully qualify the range like
Code:
roster.Range(roster.Cells(1, 1), roster.Cells(1, 2)) = some value
or
Code:
With roster
    .Range(.Cells(1, 1), .Cells(1, 2)) = some value
End With
 
Upvote 0
You need to fully qualify the range like
Code:
roster.Range(roster.Cells(1, 1), roster.Cells(1, 2)) = some value
or
Code:
With roster
    .Range(.Cells(1, 1), .Cells(1, 2)) = some value
End With

Worked like a charm. :) :)

Thanks Fluff! Merry Christmas!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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