Results 1 to 4 of 4

Thread: Trouble renaming excel worksheet.........

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    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

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Trouble renaming excel worksheet.........

    DKenny thanks. That works great! I was trying to overthink the situation. Thanks for all replies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width