activesheet error

B

BeSmart

Hi
I've done something wrong here and I can't work out what it is...
Error msg: "Compile Error: With object must be user-defined type, object,
or Variant"

Sub copylplan()
Dim mySheet As String

mySheet = ActiveSheet

With mySheet
.Copy After:=Sheets(Sheets.Count)
End With

With mySheet
.Name = "Extract Plan"
End With
End Sub
 
O

OssieMac

Hi,

Set the variable as a Worksheet then use Set in the line to assign the
ActiveSheet to the variable.

You should realize that the variable remains the original ActiveSheet; it
does not become the new ActiveSheet after the copy and therefore it is the
original worksheet that gets renamed with the following code.

Sub copylplan()

Dim mySheet As Worksheet

Set mySheet = ActiveSheet

With mySheet
.Copy After:=Sheets(Sheets.Count)
End With

With mySheet
.Name = "Extract Plan"
End With

End Sub


If you want to apply the name to the new sheet then as follows.

Sub copylplan()

Dim mySheet As Worksheet

Set mySheet = ActiveSheet

With mySheet
.Copy After:=Sheets(Sheets.Count)
End With

With ActiveSheet
.Name = "Extract Plan"
End With

End Sub


You can then still reference the original worksheet like follows.

mySheet.Activate
 

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