+ Reply to Thread
Results 1 to 9 of 9

Thread: Code to run macros on protected sheets

  1. #1
    Forum Contributor
    Join Date
    07-15-2011
    Location
    Whitby, Canada
    MS-Off Ver
    Excel 2010
    Posts
    121

    Code to run macros on protected sheets

    I am a real newbie when it comes to VBA and I am limited to copying and pasting codes I search for online - in addition to finding help here when needed **thanks to the super moderators who have already helped** so here is my current issue:

    I have a code that changes the colour of a cell (my conditional formats are maxed out)

    Sub ColorValues()
    Application.Volatile
        Dim Values As Range
        Dim cell As Variant
        Set Values = Range("A3:F28")
        For Each cell In Values
            If cell.Value = "OT" Or cell.Value = "A/P/C" _
            Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then
                cell.Font.ColorIndex = 10
                    End If
                    Next cell          
    End Sub
    Although I have [Application.Volatile] in the code, it does not actually change the colour of the cell as I go along. Perhaps I have inserted it incorrectly? So, I added a button and assigned the above macro to it - works great when the workbook is unprotected, but I get a runtime error '1004' when the workbook is locked. I have searched for and tried a variety of codes to get around this such as:


    Sub ProtectUIOnly()
    ActiveSheet.Unprotect Password:="mypassword"
    ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True
    End Sub

    But since I don't really know what I am doing, it doesn't work (runtime error '1004'). I can only assume that I have inserted it incorrectly into the existing code or in the wrong place etc. (I currently have this code as its own module and I get the runtime error)

    The solution I need is this: The workbook needs to be protected so the users do not accidentally change anything, but I might not need the 'unlock' macro if I can get the 'Application.Volatile' part to work - changing the colour as I go (this is a drop-down by the way). Any help, explanations etc. would be much appreciated. Workbook attached...

    thanks in advance
    Attached Files Attached Files
    Last edited by Greed; 08-17-2011 at 03:47 PM. Reason: Solved

  2. #2
    Forum Contributor
    Join Date
    07-15-2011
    Location
    Whitby, Canada
    MS-Off Ver
    Excel 2010
    Posts
    121

    Re: Code to run macros on protected sheets

    Apologies - I thought by typing them in, it would have the same effect - apparently not, please excuse my ignorance.

  3. #3
    Forum Guru
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    1,798

    Re: Code to run macros on protected sheets

    Greed:

    (1) You can type your code tags. I think you initially closed your code tag with [\code] instead of [/code]. (Note the orientation of the slash.

    (2) In order to run code on a protected sheet, you have to think in turns of chronology. So, you would have to first unprotect the sheet, then run the code, then reprotect it. Without doing any troubleshooting, your code should read like the following:

    Sub ProtectUIOnly()
    ActiveSheet.Unprotect Password:="mypassword"
        Dim Values As Range
        Dim cell As Variant
        Set Values = Range("A3:F28")
        For Each cell In Values
            If cell.Value = "OT" Or cell.Value = "A/P/C" _
            Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then
                cell.Font.ColorIndex = 10
                    End If
                    Next cell       
    ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True
    End Sub
    Let me know if this works and if there is anything else with which I can help.

  4. #4
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Code to run macros on protected sheets

    Quote Originally Posted by Greed View Post
    Apologies - I thought by typing them in, it would have the same effect - apparently not, please excuse my ignorance.
    You absolutely can type them in. If they are typed like:

    [code]Sub YourProcedureHere()[/code]

    there will be no problems.

    Cheers
    Please leave a message after the beep!

  5. #5
    Forum Contributor
    Join Date
    07-15-2011
    Location
    Whitby, Canada
    MS-Off Ver
    Excel 2010
    Posts
    121

    Re: Code to run macros on protected sheets

    Yes! That's just the thing, and I thank you for the explanation - I did have to alter the button to make it work as I was initially getting another error, but it works now so that's great.

    I feel like an idiot not noticing the / vs \ in my code tags though....

    Can anyone explain why the 'Application.Volatile' portion of my code isn't working for me? Is it because I have made an error, or because it is not effective on cells that are drop-downs perhaps?

    Thanks again!

  6. #6
    Forum Guru
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    1,798

    Re: Code to run macros on protected sheets

    Application.Volatile, to my understanding, applies more to user defined functions, essentially forcing a recaculation whenever a change is made to a spreadsheet. It can also slow down the functionality of the spreadsheet.

    In your case, I think you should do some research on Worksheet_Change events. These are placed into the worksheet code instead of a standard module, and can trigger a macro based on a change. For example, if you added this code directly to the worksheet module, it would run your macro ANYTIME a change is made to cell A1 in the corresponding sheet:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$A$1" Then
         ActiveSheet.Unprotect Password:="mypassword"
        Dim Values As Range
        Dim cell As Variant
        Set Values = Range("A3:F28")
        For Each cell In Values
            If cell.Value = "OT" Or cell.Value = "A/P/C" _
            Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then
                cell.Font.ColorIndex = 10
                    End If
                    Next cell       
    ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True
       
        End If
    End Sub

  7. #7
    Forum Contributor
    Join Date
    07-15-2011
    Location
    Whitby, Canada
    MS-Off Ver
    Excel 2010
    Posts
    121

    Re: Code to run macros on protected sheets

    Thanks, the explanation helps. I will continue to study up on my Excel skills - sometimes I wish I didn't have a job so I would have more time to learn I find it truely fascinating...

    A1 is a locked cell that never changes. With the code you provided, am I able to replace $A$1 with a range instead?

  8. #8
    Forum Guru
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    1,798

    Re: Code to run macros on protected sheets

    Greed,

    The TARGET refers to the cell that is being changed, so it can never truly be a range of cells. There are ways around this. For example, instead of returning the address of the target, you can search the column of the target. To trigger the macro anytime a change is made to any cell in column B, the code would read:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 2 Then
         ActiveSheet.Unprotect Password:="mypassword"
        Dim Values As Range
        Dim cell As Variant
        Set Values = Range("A3:F28")
        For Each cell In Values
            If cell.Value = "OT" Or cell.Value = "A/P/C" _
            Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then
                cell.Font.ColorIndex = 10
                    End If
                    Next cell       
    ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True
       
        End If
    End Sub
    If you want to run a macro whenever there is a change to a number of cells, you can use UNION. The following code will trigger your macro whenever you make a change to any cells in range A4:F25:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Union(Target, Range("A4:F25")) Is Nothing Then
            ActiveSheet.Unprotect Password:="mypassword"
            
            Dim Values As Range
            Dim cell As Variant
            Set Values = Range("A3:F28")
            
            For Each cell In Values
                If cell.Value = "OT" Or cell.Value = "A/P/C" _
                    Or cell.Value = "A/C" Or cell.Value = "DE-IN" Then
                    cell.Font.ColorIndex = 10
                End If
            Next cell
            
            ActiveSheet.Protect Password:="mypassword", userinterfaceonly:=True
        End If
    End Sub

  9. #9
    Forum Contributor
    Join Date
    07-15-2011
    Location
    Whitby, Canada
    MS-Off Ver
    Excel 2010
    Posts
    121

    Re: Code to run macros on protected sheets

    Thanks - unfortunately the 2nd code (referring to UNION) does not function as expected...the cells do not change colour once the corresponding codes (A/C, OT etc.) are selected - again, not sure if this is because the text is selected from a drop-down list? No worries though, the insertion of the unprotect/protect code is enabling the macro button so I'm happy!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0