Worksheet object assignment

H

Heapy

I'm trying to assign a worksheet object to a variable at the time of its
creation through a copy function, but the process always fails with an
"Object Variable Not Set" error. i.e.:

Dim ns As Worksheet
Set ns = Sheets(GrpShellTemp).Copy(Before:=Sheets(GrpShellTemp))

.... while in another module, "GrpShellTemp" is defined as a constant:

Public Const GrpShellTemp As String = "GroupShell"



When the code runs, the sheet in question IS copied and is placed before the
one being copied, however the assignment fails and the error results.

I know I can get around this by simply allowing the copy function to run
without the assignment as follows:

Sheets(GrpShellTemp).Copy Before:=Sheets(GrpShellTemp)

.... and then assign the variable:

Set ns = ActiveSheet

.... but I shouldn't have to do this, should I? Why won't the assignment work
at the time the sheet is copied?

Thx
HWH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top