Trouble renaming excel worksheet.........
This code is working beautifully except for the last step where I am trying to rename the copied worksheet to what the user chose to name it in the inputbox. Any suggestions appreciated. Error is something like "Object variable not defined". Here's my code:
Code:
Sub COPY_FROM_MainSched_TO_DoorSched()
'Declare variables
Dim SourcePath, SourceFile, SourceWbk, MSG, NewShtName As String
Dim destsheet, srcsheet As Workbook
Dim NewSht As Worksheet
'set variables
SourcePath = "H:\schedule\"
SourceFile = "Cabot, S.xls"
SourceWbk = SourcePath & SourceFile
'Disable alerts & screen updating
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Copy the sheet from the source workbook to the destination workbook
Set destsheet = ActiveWorkbook
Set srcsheet = Workbooks.Open(SourceWbk)
srcsheet.Worksheets.Copy _
after:=destsheet.Sheets(destsheet.Sheets.Count)
srcsheet.Close True
'Ask the user what they would like to name the copied sheet
NewShtName = InputBox(prompt:="Please enter a name for the new worksheet")
'Check if the user has entered a name
While NewShtName = ""
'name field has been left blank
MSG = MsgBox(prompt:="Worksheet name required!", Buttons:=vbOKOnly + vbExclamation)
NewShtName = InputBox(prompt:="Please enter a name for the new worksheet")
Wend
'Rename the worksheet
NewSht = ActiveWorkbook.Worksheets("Log (2)")
NewSht.Name = NewShtName
End Sub
Re: Trouble renaming excel worksheet.........
Don't you need a SET statement? or maybe a SET x = NEW sheet
Did you declare it? I didn't see it.
Re: Trouble renaming excel worksheet.........
If you only need to rename the sheet and not perform any further actions, then why use an object variable at all?
replacing
NewSht = ActiveWorkbook.Worksheets("Log (2)")
NewSht.Name = NewShtName
with
ActiveWorkbook.Worksheets("Log (2)").Name = NewShtName
should do it.
Re: Trouble renaming excel worksheet.........
DKenny thanks. That works great! I was trying to overthink the situation. Thanks for all replies.