Consulting

Results 1 to 6 of 6

Thread: Run time 1004 error in VBA

  1. #1
    VBAX Newbie
    Joined
    Dec 2006
    Posts
    1
    Location

    Run time 1004 error in VBA

    I am a very novice VBA user.
    I am making schedule worksheets for 3 different shifts. I want pages to name themselves based on data in a cell.
    Using View Code on a page tab, I have this entered:

    Private Sub Auto_Open()
    ActiveSheet.Name = ActiveSheet.Range("IA2").Value
    End Sub

    When building, I tried saving this in Workbook in the Project-VBA Project window but it wouldn't run when I opened the notebook. Somehow I figured out how to save it as a module under that workbook. And it ran beautifully...every time.

    Then I copied the Workbook for the other two shifts. In the Project-VBA Project window, it showed the module under each project.

    Then when I tried any of the workbooks, I got Run-Time Error "1004" Application-defined or object-defined error.

    I have tried saving the formula in a page, in the workbook, taking it out and reentering it. No luck...same error.

    Can anyone help?
    Kimberly

  2. #2
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location
    Have you tried to save the module in your personal.xls vbaproject folder and linking your macro to an excel toolbar button? The toolbar button would be visible no matter which workbook you open.

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    The Private Sub Auto_Open is for a workbook open event, ideally you want to use the Worksheets Activate, go to the worksheet code module choose worksheet from the left drop down and the Activate from the right hand, then use your statement like this [VBA]Private Sub Worksheet_Activate()
    ActiveSheet.Name = Range("A1").Value
    End Sub[/VBA]Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    VBAX Regular vonpookie's Avatar
    Joined
    Jun 2004
    Location
    Are we there yet?
    Posts
    74
    Location
    Just a note, but this was cross-posted:
    http://www.mrexcel.com/board2/viewtopic.php?t=247145

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Kimberly,
    Just a note about cross-posting. Please follow this link and read it for an explanation of cross-posting
    http://www.excelguru.ca/node/7

    Simon gave you the best solution to your problem....see attached and look at the code for sheet1(right click on the tab at the bottom of the page and select "view code")

    There are other ways to do this including formula's
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    If you want this to apply to every sheet, use this variation in the ThisWorkbook code module [VBA]Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    On Error Resume Next '< error=another sheet has same name, cell is empty or illegal character, is chart, etc
    ActiveSheet.Name = Range("A1").Value
    End Sub[/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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