Results 1 to 6 of 6

Thread: Excel VBA and Sheet Protection Problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Excel VBA and Sheet Protection Problem

    Hi all,

    I'm just putting the finishing touches to an Excel application and have started to protect the worksheets using the security function so that users can only enter values into certain cells.

    However, after doing this and then testing the worksheet I came across a very big problem! I have quite a lot of VBA code in this application that carries out a lot of calculations and then writes these to various worksheets. After I added the worksheet protection I found that whenever I try to run the VBA code I get errors stating in effect that the computer can't write the results of the VBA calculations to the worksheets as they are protected!

    This is most annoying since I only want to stop the users from entering data in the wrong cells - I don't want to stop the computer from been able to write data into them!

    In the Protect Worksheet options dialog box I've tried checking all the available options but it still can't write anything to the worksheet.

    The only solution that I can think of is to go through the code to see which cells it's trying to change and then Unlock them all in the Protection dialog box. However this would be extremely inefficient as the application is large and was developed in an ad-hoc manner and so there are thousands of cells all over the place that need writing to.

    Isn't there any way to allow the VBA code to write to a protected worksheet?

    Thanks
    -Rob

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel VBA and Sheet Protection Problem

    Your best bet I think is to unprotect the entire sheet, do your processing and
    then protect just those cells again.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: Excel VBA and Sheet Protection Problem

    Quote Originally Posted by RobDog888
    Your best bet I think is to unprotect the entire sheet, do your processing and
    then protect just those cells again.
    This is for a commericial application though, so I don't think it wise to expect people to do that every time they want to do a calculation!

    This seems to be a bit of a flaw in the protect worksheet function if it means that the VBA code can't alter the worksheets unless you unprotect every cell that it writes to.

    If I have to do this by hand then it'll take ages. I have literally thousands of cells that will need unprotecting.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel VBA and Sheet Protection Problem

    But you can Protect/UnProtect the entire sheet(s) through code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Excel VBA and Sheet Protection Problem

    use a macro!

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel VBA and Sheet Protection Problem

    Quote Originally Posted by dglienna
    use a macro!
    David, in his original post he states that ...
    Quote Originally Posted by TheRobster
    I have quite a lot of VBA code in this application that carries out a lot of calculations and then writes these to various worksheets. After I added the worksheet protection I found that whenever I try to run the VBA code I get errors stating in effect that the computer can't write the results of the VBA calculations to the worksheets as they are protected!
    He's already writting code in the VBA IDE and needs a ways to resolve the isue. A macro will not be of any help.

    TheRobster, if you look into my previous post about Protection, you should be able to resolve this. At the beginning of a function make a call to remove the protection for the sheet. Then at the end of the function turn protection back on.

    Quote Originally Posted by MS Excel HelpFile
    expression.Protect(Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)
    VB Code:
    1. ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Locking cells or hiding formulas has no effect unless the worksheet is protected.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width