Creating a Copy worksheet error when renaming it

  • Thread starter Thread starter NetWave128
  • Start date Start date
N

NetWave128

Here is my code:

Public Sub NewInterface()
Dim NewSheet As Worksheet

On Error GoTo ErrorHandler

Set NewSheet = ThisWorkbook.Worksheets("Interfac
Master").Copy(after:=ThisWorkbook.Worksheets("Interface Master"))
NewSheet.Name = "New Interface Sheet"

Exit Sub '<-- if everything runs error-free, you will exit here
BEFORE the error handler.

ErrorHandler:
MsgBox "Sorry, an error occurred." & vbCrLf & "Number: " & Err.Numbe
& vbCrLf & "Description: " & Err.Description, vbCritical, "Error"

End Su

Attachment filename: error.gif
Download attachment: http://www.excelforum.com/attachment.php?postid=46849
 
the copy method for a worksheet doesn't not return anything. You can't use
(it doesn't return a reference to the copy of the sheet).
Set NewSheet = ThisWorkbook.Worksheets("Interface
Master").Copy(after:=ThisWorkbook.Worksheets("Interface Master"))

Instead:

ThisWorkbook.Worksheets("Interface Master").Copy _
after:=ThisWorkbook.Worksheets("Interface Master")
set NewSheet = Activesheet
 

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

Back
Top