+ Reply to Thread
Results 1 to 6 of 6

'Runtime Error 1004' when trying to Rename Worksheet using vba code

  1. #1
    Registered User
    Join Date
    02-11-2013
    Location
    Belfast, Maine
    MS-Off Ver
    Excel 2003
    Posts
    4

    'Runtime Error 1004' when trying to Rename Worksheet using vba code

    There is probably a simple solution for my problem, however I am just learning VBA.

    I am using the attached code to name a worksheet. It works fine until I use a name that is already used on a worksheet in the workbook.

    I want to have the option to enter another name that is not a duplicate.

    Any suggestions would be accepted with gratitude.

    ThanksName.txt
    Name2.txt
    Last edited by Raporter65; 03-28-2013 at 09:38 AM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: 'Runtime Error 1004' when trying to Rename Worksheet using vba code

    Hi there,

    Try this:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    02-11-2013
    Location
    Belfast, Maine
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: 'Runtime Error 1004' when trying to Rename Worksheet using vba code

    Thanks Trebor76,

    I should have included the rest of the code.

    The full code is as below. Would you please suggest where to insert the code you suggested?

    Private Sub CommandButton9_Click()
    Workbooks.Open Filename:=("x:\purchasing\Current Contracts\Purchasing Workbook\JPACEMaster.xlsx")
    Windows("JPACE.xlsm").Activate
    Sheets("JPACE-SAGE").Select
    Sheets("JPACE-SAGE").Unprotect
    Range("A1:L81").Copy
    Windows("JPACEMaster.xlsx").Activate
    Sheets("Sheet1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    wsName = InputBox("Enter DO# and type of sheet", "Rename Sheet")
    If wsName <> "" Then
    With ActiveSheet
    .Name = wsName
    End With
    End If
    ActiveWindow.Zoom = 75
    ActiveSheet.Range("a1").Select
    ActiveSheet.Protect
    Dim ws As Worksheet
    Set ws = Sheets.Add
    Worksheets("Sheet1").Move _
    Before:=Worksheets("Sheet2")
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Windows("JPACE.xlsm").Activate
    ActiveSheet.Protect
    End Sub


    Thanks for your help in advance!

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: 'Runtime Error 1004' when trying to Rename Worksheet using vba code

    Untested, but try this:

    Please Login or Register  to view this content.
    You must also make sure to wrap any code you post with the appropriate tags as I have done.

    Robert

  5. #5
    Registered User
    Join Date
    02-11-2013
    Location
    Belfast, Maine
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: 'Runtime Error 1004' when trying to Rename Worksheet using vba code

    Thanks Trebor76. This worked. I was not able to add the 'Option Explicit' to the sub, however the code worked anyhow.
    Would you please explain how to wrap my code that I post with the appropriate tag? I am new to this and do not understand what you mean.

    Once again, thanks for your help in resolving my issue.

    Rob

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: 'Runtime Error 1004' when trying to Rename Worksheet using vba code

    I was not able to add the 'Option Explicit' to the sub
    Then you must be variables that aren't defined like I did with the varWSName variable. This means that these variables default to a variant type, the most expensive type (in terms of memory) of a variable.

    Would you please explain how to wrap my code that I post with the appropriate tag?
    After you copy in any code, highlight it and click on the hash # icon in the menu.

    HTH

    Robert

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1