+ Reply to Thread
Results 1 to 4 of 4

Macro does not work in Protected Worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    04-26-2014
    Location
    Illinois, USA
    MS-Off Ver
    Excel 2007
    Posts
    1

    Macro does not work in Protected Worksheet

    I have a macro in my worksheet that highlights any cell where a change has been made. Below is the macro:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
     
    
    Dim ws As Worksheet, ws2 As Worksheet
    
    Dim i As Boolean
    
    Application.ScreenUpdating = False
    
     
    
    'Create Change Log if one does not exist.
    
    i = False
    
    For Each ws In ThisWorkbook.Worksheets
    
        If ws.Name = "Change Log" Then
    
            i = True
    
            Exit For
    
        End If
    
    Next ws
    
    If Not i Then
    
       Set ws2 = ThisWorkbook.Worksheets.Add
    
       ws2.Visible = xlSheetHidden
    
       ws2.Name = "Change Log"
    
       ws2.Range("A1") = "Sheet"
    
       ws2.Range("B1") = "Range"
    
       ws2.Range("C1") = "Old Interior Color"
    
    Else
    
       Set ws2 = Sheets("Change Log")
    
    End If
    
     
    
    'Store previous color data in change log for rollback.
    
    ws2.Range("A1").Offset(ws2.UsedRange.Rows.Count, 0) = Target.Worksheet.Name
    
    ws2.Range("B1").Offset(ws2.UsedRange.Rows.Count - 1, 0) = Target.Address
    
    ws2.Range("C1").Offset(ws2.UsedRange.Rows.Count - 1, 0) = Target.Interior.Color
    
     
    
    'Change cell color to yellow.
    
    Target.Interior.Color = 13434879
    
     
    
    Application.ScreenUpdating = True
    End Sub


    .....When I protect the worksheet and lock certain cells from being touched, the macro stops working and an error pops up, asking if I wish to debug. There are certain ranges in the worksheet that can be touched but even when I make a change within those ranges, the error pops up for debugging.

    When I continue to debug it, the following portion of the macro is highlighted: Target.Interior.Color = 13434879

    Can you please advise how to run this macro in a protected worksheet?

    Also, if I only wanted to apply it to a certain range of the worksheet, how would this be done?

    Thank you in advance for the advice and help!

    Best,
    Nathaly Renderos
    Last edited by alansidman; 04-26-2014 at 10:42 PM. Reason: code tags added

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Macro does not work in Protected Worksheet

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    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,175

    Re: Macro does not work in Protected Worksheet

    'Change cell color to yellow.
    Me.Unprotect "password"
    Target.Interior.Color = 13434879
    Me.Protect "password"
     
    Application.ScreenUpdating = True

    Regards, TMS
    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


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

    Re: Macro does not work in Protected Worksheet

    You can include a code to unprotect the sheet before accessing the worksheet in the code and protect it again after the action is completed like

    ws2.unprotect and then ws2.protect

    Hope that helps.
    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.

+ 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. MAcro does not work when certain cells in a work sheet are protected
    By Unnati in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-10-2012, 03:38 AM
  2. [SOLVED] Macros do not work when worksheet is protected
    By a_williams in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-24-2012, 02:36 AM
  3. How to make TAB key work in a protected worksheet
    By Sri Ram in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-18-2006, 02:15 AM
  4. Replies: 2
    Last Post: 07-11-2006, 11:15 PM
  5. Can't Get Objects To Work In A Protected Worksheet
    By OMMBoy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-27-2006, 01:35 PM

Tags for this Thread

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