+ Reply to Thread
Results 1 to 9 of 9

Unlock & Lock Sheets Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    12-21-2016
    Location
    Minnesota, USA
    MS-Off Ver
    2010
    Posts
    11

    Unlock & Lock Sheets Macro

    Hello all,

    I am trying to find a solution to my workbook, I have a macro that moves entire rows between tabs that I created for my work place (with your guy's help). However at current state the macro will not work if the given sheet (tab) is locked, I am looking for a work around by either ignoring the locked tabs (ideally) or unlocking and then locking the individual tab post the movement.

    Here is the code I am using:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim rng As Range
            Set rng = Target.Parent.Range("O2:O1000")
                If Target.Count > 1 Then Exit Sub
                If Intersect(Target, rng) Is Nothing Then Exit Sub
                Select Case Target.Text
                    Case "Boxing"
                        Target.EntireRow.Copy Sheets("Boxing").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "HPC"
                        Target.EntireRow.Copy Sheets("HPC").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Slipcoat"
                        Target.EntireRow.Copy Sheets("Slipcoat").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Innova"
                        Target.EntireRow.Copy Sheets("Innova").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "EmboPoly"
                        Target.EntireRow.Copy Sheets("EmboPoly").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Complete"
                        Target.EntireRow.Copy Sheets("Complete").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Project Hopper"
                        Target.EntireRow.Copy Sheets("Project Hopper").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Cancel"
                        Target.EntireRow.Copy Sheets("Cancel").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case Is = ""
                        Target.EntireRow.Copy Sheets("Idea Entry").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                End Select
    End Sub
    Any suggestions? Thank you for your time! :-)

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Unlock & Lock Sheets Macro

    This would do it I think... but it does assume that the sheet is protected without password, you will have to add the password in quotes behind the .unprotect and .protect lines if you need to specify one.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim rng As Range
        Dim isprotected As Boolean
            Set rng = Target.Parent.Range("O2:O1000")
                If Target.Count > 1 Then Exit Sub
                If Intersect(Target, rng) Is Nothing Then Exit Sub
                isprotected = Sh.ProtectContents
                If isprotected = True Then Sh.Unprotect
                            Select Case Target.Text
                    Case "Boxing"
                        Target.EntireRow.Copy Sheets("Boxing").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "HPC"
                        Target.EntireRow.Copy Sheets("HPC").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Slipcoat"
                        Target.EntireRow.Copy Sheets("Slipcoat").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Innova"
                        Target.EntireRow.Copy Sheets("Innova").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "EmboPoly"
                        Target.EntireRow.Copy Sheets("EmboPoly").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Complete"
                        Target.EntireRow.Copy Sheets("Complete").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Project Hopper"
                        Target.EntireRow.Copy Sheets("Project Hopper").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Cancel"
                        Target.EntireRow.Copy Sheets("Cancel").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case Is = ""
                        Target.EntireRow.Copy Sheets("Idea Entry").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                End Select
                If isprotected = True Then Sh.Protect
    End Sub
    If you want to ignore locked tabs (meaning skip the code for any locked sheet), then change
    If isprotected = True Then Sh.Unprotect
    to
    If isprotected = True Then Exit Sub
    and remove
    If isprotected = True Then Sh.Protect
    from the end
    Last edited by Arkadi; 01-24-2017 at 10:20 AM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    12-21-2016
    Location
    Minnesota, USA
    MS-Off Ver
    2010
    Posts
    11

    Re: Unlock & Lock Sheets Macro

    Hello Arkadi,

    In what format would I type the password behind the .unprotect and .protect (I am using "temp" as the current password). Sorry I am a noob when it comes to this.

    Thank you!

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Unlock & Lock Sheets Macro

    just in quotes behind the command:

    If isprotected = True Then Sh.Unprotect "temp"
    and
    If isprotected = True Then Sh.Protect "temp"
    or you can use the actual name of the parameter but don't have to:

    If isprotected = True Then Sh.Unprotect Password:="temp"
    and
    If isprotected = True Then Sh.Protect Password:="temp"

  5. #5
    Registered User
    Join Date
    12-21-2016
    Location
    Minnesota, USA
    MS-Off Ver
    2010
    Posts
    11

    Re: Unlock & Lock Sheets Macro

    When I am using this, I am getting the following error:

    Run-time error '1004'

    The cell or chart that you are trying to change is protected and therefore read-only

    To modify a protected cell or chart, first remove protection using the unprotected sheet command (review tab, changes group). You may be prompted for a password.
    I used the following code:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim rng As Range
        Dim isprotected As Boolean
            Set rng = Target.Parent.Range("O2:O1000")
                If Target.Count > 1 Then Exit Sub
                If Intersect(Target, rng) Is Nothing Then Exit Sub
                isprotected = Sh.ProtectContents
                If isprotected = True Then Sh.Unprotect Password:="temp"
                            Select Case Target.Text
                    Case "Boxing"
                        Target.EntireRow.Copy Sheets("Boxing").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "HPC"
                        Target.EntireRow.Copy Sheets("HPC").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Slipcoat"
                        Target.EntireRow.Copy Sheets("Slipcoat").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Innova"
                        Target.EntireRow.Copy Sheets("Innova").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "EmboPoly"
                        Target.EntireRow.Copy Sheets("EmboPoly").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Complete"
                        Target.EntireRow.Copy Sheets("Complete").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Project Hopper"
                        Target.EntireRow.Copy Sheets("Project Hopper").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case "Cancel"
                        Target.EntireRow.Copy Sheets("Cancel").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                    Case Is = ""
                        Target.EntireRow.Copy Sheets("Idea Entry").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                End Select
                If isprotected = True Then Sh.Protect Password:="temp"
    End Sub

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Unlock & Lock Sheets Macro

    wow I'm stupid... just realized the sheets that are protected are others, not necessarily the one being changed.... let me get back to you in a few.

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Unlock & Lock Sheets Macro

    originally I took care of the scenario where the source sheet was locked, and the row deletion would be stopped by the protection, but it did not account for each individual other sheet (destinations) to be protected. Give this a try (assumes all sheets have temp as the password, change those as needed.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim rng As Range
        Dim isprotected As Boolean
        Dim isprotected2 As Boolean
            Set rng = Target.Parent.Range("O2:O1000")
                If Target.Count > 1 Then Exit Sub
                If Intersect(Target, rng) Is Nothing Then Exit Sub
                isprotected2 = Sh.ProtectContents
                If isprotected2 = True Then Sh.Unprotect Password:="temp"
                            Select Case Target.Text
                    Case "Boxing"
                        isprotected = Sheets("Boxing").ProtectContents
                        If isprotected = True Then Sheets("Boxing").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("Boxing").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("Boxing").Protect Password:="temp"
                    Case "HPC"
                        isprotected = Sheets("HPC").ProtectContents
                        If isprotected = True Then Sheets("HPC").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("HPC").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("HPC").Protect Password:="temp"
                    Case "Slipcoat"
                        isprotected = Sheets("Slipcoat").ProtectContents
                        If isprotected = True Then Sheets("Slipcoat").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("Slipcoat").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("Slipcoat").Protect Password:="temp"
                    Case "Innova"
                        isprotected = Sheets("Innova").ProtectContents
                        If isprotected = True Then Sheets("Innova").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("Innova").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("Innova").Protect Password:="temp"
                    Case "EmboPoly"
                        isprotected = Sheets("EmboPoly").ProtectContents
                        If isprotected = True Then Sheets("EmboPoly").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("EmboPoly").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("EmboPoly").Protect Password:="temp"
                    Case "Complete"
                        isprotected = Sheets("Complete").ProtectContents
                        If isprotected = True Then Sheets("Complete").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("Complete").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("Complete").Protect Password:="temp"
                    Case "Project Hopper"
                        isprotected = Sheets("Project Hopper").ProtectContents
                        If isprotected = True Then Sheets("Project Hopper").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("Project Hopper").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("Project Hopper").Protect Password:="temp"
                    Case "Cancel"
                        isprotected = Sheets("Cancel").ProtectContents
                        If isprotected = True Then Sheets("Cancel").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("Cancel").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("Cancel").Protect Password:="temp"
                    Case Is = ""
                        isprotected = Sheets("Idea Entry").ProtectContents
                        If isprotected = True Then Sheets("Idea Entry").Unprotect Password:="temp"
                        Target.EntireRow.Copy Sheets("Idea Entry").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                        Target.EntireRow.Delete
                        If isprotected = True Then Sheets("Idea Entry").Protect Password:="temp"
                End Select
                If isprotected2 = True Then Sh.Protect Password:="temp"
    End Sub

  8. #8
    Registered User
    Join Date
    12-21-2016
    Location
    Minnesota, USA
    MS-Off Ver
    2010
    Posts
    11

    Re: Unlock & Lock Sheets Macro

    Thank you very much Arkadi! this worked like a charm :-)

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Unlock & Lock Sheets Macro

    Glad to hear it
    Thanks for marking as solved, and for the rep!

+ 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. [SOLVED] unlock sheets apply macro then lock sheets again
    By k1dr0ck in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2015, 10:21 PM
  2. Lock and unlock sheets with password , diffrent usernames different actions
    By Barry Engelbrecht in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-20-2013, 07:06 AM
  3. Macro to lock/unlock computer
    By wfm007 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2013, 02:18 PM
  4. VBA to unlock cells, run macro then re-lock them
    By tiger01 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-05-2013, 02:24 AM
  5. Lock and Unlock sheets in a shared workbook?
    By Dulanic in forum Excel General
    Replies: 0
    Last Post: 02-15-2011, 01:24 PM
  6. Lock and Unlock Sheets based on Cell values
    By Mujahid_Sgd in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-02-2011, 05:18 AM
  7. using VB code to unlock and lock different areas on multiple sheets
    By Healthwatch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2010, 11:12 PM

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