+ Reply to Thread
Results 1 to 4 of 4

Macro initially has runtime error 1004 but works on 2nd attempt

  1. #1
    Registered User
    Join Date
    08-06-2013
    Location
    Thetford, England
    MS-Off Ver
    Excel 2013 / 365
    Posts
    8

    Macro initially has runtime error 1004 but works on 2nd attempt

    Hello All

    Please could I request a little assistance with a macro I have cobbled together. I have it linked to a button & initially it has a runtime error 1004, but will then work on 2nd attempt. I have the worksheet protected & in the vba, I unprotect it & then protect it again but if I run it without the sheet being protected it works fine which leads me to believe the problem is to do with the protection. My code is as follows

    [code]
    Sub Retrieve_Ref_BOM()
    Range("J13:M14").Select
    ActiveCell.Hyperlinks(1).Follow
    Sheets("COMPLETE BOM").Select
    ActiveSheet.Unprotect
    Columns("P:V").Select
    Selection.EntireColumn.Hidden = False
    ActiveSheet.Range("$A$6:$U$3000").AutoFilter Field:=17
    Range("A3:L3000").Select
    Selection.Copy
    ActiveWorkbook.Windows(1).Visible = False
    ActiveSheet.Unprotect
    Range("G24:R3021").Select
    ActiveSheet.Paste Link:=True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
    , AllowInsertingHyperlinks:=True, AllowFiltering:=True
    Range("J13:M14").Select
    ActiveCell.Hyperlinks(1).Follow
    Application.CutCopyMode = False
    ActiveSheet.Range("$A$6:$U$3000").AutoFilter Field:=17, Criteria1:="TRUE"
    Columns("Q:U").Select
    Selection.EntireColumn.Hidden = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
    , AllowFiltering:=True
    ActiveWorkbook.Save
    ActiveWindow.Close
    Filter_BOM
    ActiveSheet.Range("M16:M17").Select
    End Sub
    [code/]

    I also then has this in the Thisworkbook section

    [code]
    Private Sub Workbook_Open()
    Sheet1.Unprotect
    Sheet1.Protect , Userinterfaceonly:=True, AllowInsertingHyperlinks:=True, AllowFiltering:=True
    Sheet1.EnableSelection = xlUnlockedCells
    Sheet1.EnableOutlining = True
    End Sub
    [code/]

    When first run the error comes up & debugs on the line ActiveSheet.Paste Link:=TRUE

    Any assistance with this would be greatly appreciated

    Many Thanks in advance

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,623

    Re: Macro initially has runtime error 1004 but works on 2nd attempt

    I suggest you take out all the Protect and Unprotect lines of code in the main subroutine. Just leave the Workbook Open code.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-06-2013
    Location
    Thetford, England
    MS-Off Ver
    Excel 2013 / 365
    Posts
    8

    Re: Macro initially has runtime error 1004 but works on 2nd attempt

    Yep that sorted it, thank you so much, I thought it would be something relatively simple, shows my vba in-experience lol

    Again Thank you TMS

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,623

    Re: Macro initially has runtime error 1004 but works on 2nd attempt

    You're welcome. Thanks for the rep.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Error 462 - Macro works every 3rd attempt
    By JoePick87 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-24-2019, 07:14 AM
  2. Replies: 0
    Last Post: 03-05-2019, 03:14 PM
  3. WorksheetFunction.Vlookup Runtime Error 1004, But works in Excel Sheet.
    By hansaaa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-28-2018, 11:52 AM
  4. macro used to work but now runtime error 1004
    By clammastak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-06-2016, 03:22 PM
  5. keep getting Runtime error 1004 in my VBA Macro and not sure why
    By Pimp_mentality in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-03-2015, 04:29 PM
  6. Macro - Runtime Error 1004
    By Evian in forum Excel Charting & Pivots
    Replies: 10
    Last Post: 10-08-2008, 04:54 AM
  7. Excel 2003 Macro Error - Runtime error 1004
    By Cow in forum Excel General
    Replies: 2
    Last Post: 06-07-2005, 09:05 AM

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