+ Reply to Thread
Results 1 to 34 of 34

VBA Producing Error Code 1004

  1. #1
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    VBA Producing Error Code 1004

    Can anyone help with this VBA issue .....

    I have 7 sheets in a workbook (1 data table and 6 worksheets housing pivot tables/charts (PivotTable1) all running off the data table in sheet 1).

    I am trying to write this code to enable pivot table refresh when data is inputted into the data table followed by a protection of the sheet(s). The code will unprotect the sheet prior to the refresh before protection after the refresh.

    Code below:

    Module1:

    Please Login or Register  to view this content.
    ThisWorkbook:

    Please Login or Register  to view this content.
    (nothing within the individual sheet code boxes)

    When I input data into the data table then click on the immediate sheet following the data table (sheet 2), the pivot refreshes fine and protects. However, as I click on the other sheets I get the following error code:

    Run-time error '1004'

    That command cannot be performed while a protected sheet contains another pivot table report based on the same source data.

    To remove protection from the sheet that has the other report, click the sheet tab, and then click unprotect sheet (review tab, changes group). Then try the command again.

    I am able to click on 'End', 'Debug' and 'Help'.

    Clicking on End allows the sheet refresh, however I still get the error on the pages when clicking on them.

    The debug highlights 'pvt.RefreshTable' in the refresh code in Module1.

    It seems the error is a direct result of the sheets already being protected somehow (a beginners assumption).

    Can anyone help with the problem?

    Thanks

    Luke
    Last edited by luke.walker; 06-28-2014 at 04:08 PM.

  2. #2
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Develop VBA

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  3. #3
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: Develop VBA

    sktneer - post adjusted - THANKS !

    Quote Originally Posted by sktneer View Post
    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: VBA Producing Error Code 1004

    Thanks for changing the Thread Title.

  5. #5
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    No problem,

    Are you able to help with my problem?

    Quote Originally Posted by sktneer View Post
    Thanks for changing the Thread Title.

  6. #6
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    You could try code like this, but its not all that optimized with the loop statements

    Please Login or Register  to view this content.
    Regards,
    Rudi

  7. #7
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi,

    Thank you for the reply - where should I place the code (e.g. module 1 etc.)

    What code should go in ThisWorkbook?

    Regards,

    Quote Originally Posted by RudiS View Post
    You could try code like this, but its not all that optimized with the loop statements

    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    Hi,

    Open VBA
    Double click on the ThisWorkbook module (to open the main module on the right)
    Then just copy the code "as is" into that module.
    The Event handlers are already set in the code, so VBA will know when to run the macro.

    1.jpg

  9. #9
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi,

    It work NEARLY perfectly!!

    The Macro is doing exactly what I want it to do (thanks for that - I have asked so many people to try and solve it which has proved fruitless!) - However, when I click back on the data table I get the following error:

    Run-time error '1004':

    Unable to get the PivotTables property of the Worksheet class

    ..... Debug highlights 'Sh.PivotTables(1).PivotCache.Refresh' within the code .....

    I have 7 sheets coded as follows:
    • Sheet 1 (Data Table); Sheet 13 (PT1); Sheet 2 (PT2); Sheet 3 (PT3); Sheet 4 (PT4); Sheet 5 (PT5); Sheet 8 (PT6); Sheet 9 (PT7)

    Does the code need to be adjusted to compensate for this?

    Thanks


    Quote Originally Posted by RudiS View Post
    Hi,

    Open VBA
    Double click on the ThisWorkbook module (to open the main module on the right)
    Then just copy the code "as is" into that module.
    The Event handlers are already set in the code, so VBA will know when to run the macro.

    Attachment 328635

  10. #10
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    Hi Luke,

    Try this adjustment:

    Please Login or Register  to view this content.
    or modify Sheet1 to exactly what the name of the sheet (with the data table) is...

    BTW: Glad is is almost working
    Last edited by RudiS; 06-28-2014 at 05:12 PM.

  11. #11
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    Sorry....that was supposed to be

    If Sh.Name <> "Sheet1" Then

  12. #12
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    Please note these caveats:

    1. The data table sheet must be the first sheet in the workbook
    2. Ensure there are no hidden sheets
    3. The name of sheet 1 must be as in the code line : If Sh.Name <> "Sheet1" Then

  13. #13
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi,

    Apologies for my ignorance,

    The code I tried to enter based on your post is as follows:

    Please Login or Register  to view this content.
    However, that doesn't seem to be working (Macro stopped altogether)

    Could you provide image like before as an example please?

    Also with your previous code - when the refresh is occurring ... it flicks through all of the sheets before returning to the sheet I originally clicked on - is there a way to avoid that and instead produce a smoother refresh action?

    Thanks for all your time

    Quote Originally Posted by RudiS View Post
    Hi Luke,

    Try this adjustment:

    Please Login or Register  to view this content.
    or modify Sheet1 to exactly what the name of the sheet (with the data table) is...

    BTW: Glad is is almost working

  14. #14
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi - I changed 'Sheet1' to 'Data Table' and that has now solved the issue :-D

    Is there a way to avoid that flicker during refresh?

    If so then that is PERFECT and I owe you big time !!!

    Quote Originally Posted by RudiS View Post
    Sorry....that was supposed to be

    If Sh.Name <> "Sheet1" Then

  15. #15
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    This is untested, by usually switching off the ScreenUpdating property resolves screen flicker.

    Try the following version:

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi,

    It works like a dream,

    I can not thank you enough :-D

    Quote Originally Posted by RudiS View Post
    This is untested, by usually switching off the ScreenUpdating property resolves screen flicker.

    Try the following version:

    Please Login or Register  to view this content.

  17. #17
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    I'm glad it worked out well and happy to assist
    Cheers

  18. #18
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi,

    Sorry to be a complete nuisance ....

    I just wondered whether you know what this issue was on the other database I had .... I have the exact same database saved in another folder (with a couple less sheets housing pivots).

    I have put your code in that to get it to work the same, which it does and is fine. However, when I open the workbook initially I get the following message appear everytime:

    That command cannot be performed while a protected sheet contains another PivotTable report based on the same source data etc. etc

    Just the message you get when a sheet is protected - I can click OK and things are fine, but its just a bit annoying - do you know a way around this?

    Thanks again

  19. #19
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: VBA Producing Error Code 1004

    @Rudis

    Can it be written as the below

    Please Login or Register  to view this content.

  20. #20
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    The trick is to prevent the code from running the refresh pivot line unless a sheet with a pivot table is selected. Then if a sheet with a pivot table is selected, it must ensure that ALL sheets with pivots are unprotected before the refresh line runs. Since ALL pivots are linked to the same table source, when one pivot refreshes the others linked to that same source will too, and if that sheet is still protected it will debug. So the following code will hopefully take note of these scenarios and handle these various conditions effectively.

    Give this version a try:
    It seems bloated but I have tried to trim it down as far as possible whilst ensuring that the various conditions are evaluated and prepped.

    Please Login or Register  to view this content.
    @sktneer
    Thanks for that suggestion.
    I initially built it into my new code, but than had to remove it again as clashed with the events at the time...

  21. #21
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Morning Rudi,

    On the new database I removed all passwords. I then entered your new suggestion into ThisWorkbook. I saved, closed and reopened to find the following error:

    'Cannot edit pivot table on protected sheet'.

    I don't know why the one database works perfectly yet this one is having trouble?

    Thanks

    Quote Originally Posted by RudiS View Post
    The trick is to prevent the code from running the refresh pivot line unless a sheet with a pivot table is selected. Then if a sheet with a pivot table is selected, it must ensure that ALL sheets with pivots are unprotected before the refresh line runs. Since ALL pivots are linked to the same table source, when one pivot refreshes the others linked to that same source will too, and if that sheet is still protected it will debug. So the following code will hopefully take note of these scenarios and handle these various conditions effectively.

    Give this version a try:
    It seems bloated but I have tried to trim it down as far as possible whilst ensuring that the various conditions are evaluated and prepped.

    Please Login or Register  to view this content.
    @sktneer
    Thanks for that suggestion.
    I initially built it into my new code, but than had to remove it again as clashed with the events at the time...

  22. #22
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    Morning to you too!

    Hmmm...
    If you say you unprotected all the sheets, then this code should not debug?
    Are there hidden sheets in the workbook?

  23. #23
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    There is a hidden sheet which the pivots vLookup from for a calculation - but that same sheet is in the other database that we have got working - plus I have unprotected all the sheets and still get that error on opening?

    If I unprotect all - save (using your most recent code) and open - things work - the 3 sheets with pivots on refresh, however the data table remains unprotected - If I protect the sheets, I get the error seen in the image.

    There isn't a debug error - its just a simple cannot refresh pivot on protected sheet.

    Do you want to take a look at the sheet?

    Protected Sheets.jpg
    Quote Originally Posted by RudiS View Post
    Morning to you too!

    Hmmm...
    If you say you unprotected all the sheets, then this code should not debug?
    Are there hidden sheets in the workbook?

  24. #24
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: VBA Producing Error Code 1004

    @luke.walker

    Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.

  25. #25
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi,

    I have actually just noticed that on the other worksheet that we managed to get working - the hidden sheet that is on there comes up with an error/debug message, however because it was hidden, it seems that your code was bypassing the error and allowing for it to work?

  26. #26
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    @sktneer - My sincere apologies - I am new to the forum and therefore learning the ropes - it won't happen again!

    Thanks

    Quote Originally Posted by sktneer View Post
    @luke.walker

    Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.

  27. #27
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: VBA Producing Error Code 1004

    It's OK. Thanks.

  28. #28
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi,

    Apologies,

    I am new to it all - have replied anyway - Would it be worthwhile sending the workbooks across to you? It seems like your code is making things run great (Data Table and 6 sheets with pivots), however this hidden sheet (predicted height chart) is a table with data in that is used by the data table to produce a calculated value - does the code need to be adapted to compensate for that issue in the original workbook?

    Still doesn't resolve the issue in the second workbook however :-(

    Thanks for your help I really appreciate it

  29. #29
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    Sure, feel free to provide me access to the file if that is OK. It will help to resolve it by seeing the actual issue. Just tell me what/how to recreate the error so I can check for why the code bombs out.
    BTW: Ther error message in your previous post is not a VBA error as you mentioned. Maybe adding the line: Application.DisplayAlerts = False into the beginning of the macro can suppress that issue??? Will need to test it though!

  30. #30
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi how can I provide you with access to my workbook(s)?

    Sorry for ignorance

  31. #31
    Registered User
    Join Date
    07-17-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: VBA Producing Error Code 1004

    Rudi my email is as follows if you want me to simply send them over that way:

    [email protected]

  32. #32
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    No worries

    Usually persons upload files to a file sharing site and then the recipient can download it.
    If you do not use this option you can email it to me at the following address: [email protected]

  33. #33
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    Email is fine... I uploaded my address too...

  34. #34
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Producing Error Code 1004

    I have emailed the files back to you.
    Please verify if the error at startup is eliminated.
    TX

+ 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. Develop Organization Structure
    By kcasey1318 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-16-2013, 06:38 PM
  2. Trying to develop a bell curve model
    By JoshD75 in forum Excel General
    Replies: 2
    Last Post: 04-25-2009, 01:44 AM
  3. further develop IF(A2=A1+1;B1+1;1)
    By medicine stud in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-29-2007, 06:51 PM
  4. How do I develop a forum in Excel?
    By cloud in forum Excel General
    Replies: 0
    Last Post: 03-10-2006, 02:45 AM
  5. [SOLVED] Need help for develop Excel 2003 Add-ins
    By JohnLi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-19-2005, 04:06 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