+ Reply to Thread
Results 1 to 11 of 11

How do I Protect my sheet with VBA

  1. #1
    Registered User
    Join Date
    06-26-2024
    Location
    Houston, Texas
    MS-Off Ver
    365
    Posts
    20

    How do I Protect my sheet with VBA

    I'm having a problem protecting my sheet with a macro. Below is my understanding of the recommended solution, but it is not working (I'm very green with VBA!). I get run-time error 1004 'Unable to set the Hidden Property of the Range class.' Ultimately, I need to protect the sheet but keep unlocked a macro field that hides columns depending on the selected value. Thanks in advance!

    Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveWorkbook.Unprotect "my.password"
    Dim a As Variant, b As String
    If Target.Column = 2 And Target.Row = 5 Then
    b = Target.Value2
    With Range("d8:ab8")
    Application.ScreenUpdating = False
    .EntireColumn.Hidden = (b <> "Everyone")
    If b <> "Everyone" Then
    For Each a In .Cells
    If a = b Then a.EntireColumn.Hidden = False
    Next
    Application.ScreenUpdating = True
    End If
    Application.ScreenUpdating = True
    End With
    End If
    ActiveWorkbook.Protect "my.password"
    End Sub

  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
    48,577

    Re: How do I Protect my sheet with VBA

    You are unprotecting and protecting the workbook. You need to be unprotecting and protecting the worksheet where you want to make changes.
    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
    06-26-2024
    Location
    Houston, Texas
    MS-Off Ver
    365
    Posts
    20

    Re: How do I Protect my sheet with VBA

    Thank you. When I update the code to protect the sheet I receive a "Compile Error. Variable Not defined" error with every field edit/entry (whether the field is locked or not)

    Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveWorksheet.Unprotect "my.password"
    Dim a As Variant, b As String
    If Target.Column = 2 And Target.Row = 5 Then
    b = Target.Value2
    With Range("d8:ab8")
    Application.ScreenUpdating = False
    .EntireColumn.Hidden = (b <> "Everyone")
    If b <> "Everyone" Then
    For Each a In .Cells
    If a = b Then a.EntireColumn.Hidden = False
    Next
    Application.ScreenUpdating = True
    End If
    Application.ScreenUpdating = True
    End With
    End If
    ActiveWorksheet.Protect "my.password"
    End Sub

  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
    48,577

    Re: How do I Protect my sheet with VBA

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.

  5. #5
    Registered User
    Join Date
    06-26-2024
    Location
    Houston, Texas
    MS-Off Ver
    365
    Posts
    20

    Re: How do I Protect my sheet with VBA

    Last edited by BSkill; 06-28-2024 at 03:03 PM.

  6. #6
    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
    48,577

    Re: How do I Protect my sheet with VBA

    This works for me. You'll need to manually unprotect the sheet, re-protect it with your own password, and edit the code to use the new password. Make sense?

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    06-26-2024
    Location
    Houston, Texas
    MS-Off Ver
    365
    Posts
    20

    Re: How do I Protect my sheet with VBA

    Thank you so much! That worked :-)

  8. #8
    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
    48,577

    Re: How do I Protect my sheet with VBA

    You're welcome. Thanks for the rep.


    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  9. #9
    Registered User
    Join Date
    06-26-2024
    Location
    Houston, Texas
    MS-Off Ver
    365
    Posts
    20

    Re: How do I Protect my sheet with VBA

    Happy to do this, but I do not see a SOLVED option under Thread Tools

  10. #10
    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
    48,577

    Re: How do I Protect my sheet with VBA

    Should be there on the opening post.Attachment 873182
    Attached Images Attached Images

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,013

    Re: How do I Protect my sheet with VBA

    Quote Originally Posted by BSkill View Post
    Happy to do this, but I do not see a SOLVED option under Thread Tools
    Can you show me a screen shot of what you see in the Thread Tools dropdown? If you do not have the "Mark this thread as solved..." option I can do it for you, and also report it as a bug to our tech support team.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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. How to hide & unhide protect sheet when Protect Workbook is active
    By VisionSmart in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-19-2021, 06:50 AM
  2. [SOLVED] macro sheet protect / no protect
    By max_max in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-14-2016, 02:35 PM
  3. Excel 2013 - Protect Range of Cells without using Protect Sheet Button
    By caf20012 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-23-2014, 04:50 AM
  4. Edit existing marco to unlock protect sheet, remove set criteria and re protect sheet
    By summer2010 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2013, 09:29 AM
  5. Protect Sheet & Password Protect Code
    By KLahvic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-13-2009, 04:08 PM
  6. Protect Workbook Vs Protect Sheet
    By Poor_pakistani in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 05-25-2006, 09:10 AM
  7. Replies: 0
    Last Post: 05-10-2006, 10:35 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