+ Reply to Thread
Results 1 to 17 of 17

VBA code not working with protected sheets

  1. #1
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    VBA code not working with protected sheets

    Hi, I am using code to change the colour of cells in a 2003 workbook. It works fine as long as the sheet is unprotected, but as soon as I add protection I get:
    Run-time error '1004': Unable to set the ColorIndex property of the interior class.
    Does anyone have any ideas how to get round this? The code I am using is:

    Please Login or Register  to view this content.

    Thanks, Mrs T
    Last edited by jeffreybrown; 07-31-2012 at 11:53 AM. Reason: Please use code tags

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,317

    Re: VBA code not working with protected sheets

    You need to run an unprotect before the code and at the end protect the sheet again.

    Please Login or Register  to view this content.
    HTH
    Regards, Jeff

  3. #3
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA code not working with protected sheets

    Thank you very much..and sorry about the code tags

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,317

    Re: VBA code not working with protected sheets

    No problem.

    You are very welcome...Glad it works for you and thanks for the feedback

  5. #5
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA code not working with protected sheets

    I'm now getting a Runtime error '9': Subscript out of range.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,317

    Re: VBA code not working with protected sheets

    Is it possible to attach the offending workbook?

  7. #7
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA code not working with protected sheets

    If I upload a copy of the page I'm working in will that help? The workbook is 17.9MB!

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,317

    Re: VBA code not working with protected sheets

    Yes, just the single worksheet would suffice.

  9. #9
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA code not working with protected sheets

    I do not get the error when I run you code.

    However: I had to disable events to run your code. You are changing value of a cell in the worksheet_change event! This will get you in infinite loop!

    You need to add application.enableevents = false at the begining of you code and =true at the end
    Last edited by vandan_tanna; 07-31-2012 at 01:46 PM.
    Regards,
    Vandan

  10. #10
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA code not working with protected sheets

    colours.xls

    It's putting the colour in now in this copy sheet, but I'm back to Run-time error 1004!
    Thanks for your help,
    Mrs T

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,317

    Re: VBA code not working with protected sheets

    I have to step away for a few minutes, but curious why you are using this code instead of the code I provided in the other thread?

    Doesn't seem like you need to loop and why check for formulas when there aren't any (at least I can't see them).

  12. #12
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA code not working with protected sheets

    try my suggestion in post #9...I think that will fix it

  13. #13
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA code not working with protected sheets

    colours2.xls

    Hi, Here's my other version - I tried it with both codes that were suggested and got myself into a pickle with both, so it's obviously something I'm doing. I have no idea why I'm looping or checking for formulas, other than the names are imported from another page in the full workbook.
    @Vandan - thank you for trying to help, but I haven't got a clue what you mean!

  14. #14
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA code not working with protected sheets

    This is what I meant

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA code not working with protected sheets

    Thanks Vandan, I pasted in your code, but again as soon as I password protect the sheet I get Run-time error 1004: Unable to set the ColorIndex property of the interior class. It works great without the password protection.

  16. #16
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA code not working with protected sheets

    Are you sure you are unprotecting first like Jeffreybrown's post #2? I have a feeling you are protecting it in stead of unprotecting.
    Following works for me...

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    05-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA code not working with protected sheets

    That's it! Thank you

+ 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