+ Reply to Thread
Results 1 to 2 of 2

[SOLVED] Macro to Unprotect Sheet, but on Protection not Selecting Correct Filters

  1. #1
    Registered User
    Join Date
    06-28-2023
    Location
    Wolverhampton, UK
    MS-Off Ver
    Excel 2013
    Posts
    2

    Smile [SOLVED] Macro to Unprotect Sheet, but on Protection not Selecting Correct Filters

    Hi All,

    I'm using this macro, which I got from a thread on here, but when it protects the sheet again, it only ticks the first twp boxes, 'Select locked and unlocked cells'. I need to to protect what originally was selected, which was select locked and unlocked cells, format cells columns and rows, and also sort and use auto filter.

    I'm new to macros, so not entirely sure this can be done?

    Here is the code I'm using:

    Option Explicit
    Sub findandreplace()
    Dim fStr As String
    Dim tStr As String
    Dim myRng As Range
    Dim myUnlockedCells As Range
    Dim myCell As Range
    Dim wks As Worksheet
    Dim myPWD As String

    myPWD = "Password"

    Set wks = ActiveSheet

    With wks

    If .ProtectContents _
    Or .ProtectDrawingObjects _
    Or .ProtectScenarios Then
    'keep going
    Else
    MsgBox "Sheet is unprotected--just use Edit|Replace!"
    Exit Sub
    End If

    Set myRng = Nothing
    On Error Resume Next
    Set myRng = Intersect(Selection, .UsedRange)
    On Error GoTo 0

    If myRng Is Nothing Then
    MsgBox "Please select cells in the used range"
    Exit Sub
    End If

    For Each myCell In myRng.Cells
    If myCell.Locked = False Then
    If myUnlockedCells Is Nothing Then
    Set myUnlockedCells = myCell
    Else
    Set myUnlockedCells = Union(myUnlockedCells, myCell)
    End If
    End If
    Next myCell

    If myUnlockedCells Is Nothing Then
    MsgBox "No unlocked cells in the selected range"
    Exit Sub
    End If

    fStr = InputBox(Prompt:="Change what")
    If Trim(fStr) = "" Then
    Exit Sub
    End If

    tStr = InputBox(Prompt:="To what")
    If Trim(tStr) = "" Then
    Exit Sub
    End If

    .Unprotect Password:=myPWD

    If myUnlockedCells.Cells.Count = 1 Then
    Set myUnlockedCells _
    = Union(myUnlockedCells, _
    .Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1))
    End If

    On Error Resume Next
    myUnlockedCells.Cells.Replace what:=fStr, _
    replacement:=tStr, lookat:=xlPart, _
    searchorder:=xlByRows, MatchCase:=False
    If Err.Number <> 0 Then
    MsgBox "An error occurred during the mass change!"
    Err.Clear
    End If
    On Error Goto 0

    .Protect Password:=myPWD

    End With

    End Sub


    Thank you!

    Diane
    Last edited by Blue_Eyed_Di; 07-07-2023 at 11:27 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Macro to Unprotect Sheet, but on Protection not Selecting Correct Filters

    Hi Diane,

    Try the below;

    Towards the end of your code, select:

    Please Login or Register  to view this content.
    Replace with:

    Please Login or Register  to view this content.
    If your Question is answered; please mark it SOLVED. If you are happy with a member's solution, say 'Thanks' and click the 'Star' to Add Reputation.

+ 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] Toggle Button triggers sheet protection warning, despite using Unprotect in the macro
    By _christoph in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-15-2023, 07:02 AM
  2. [SOLVED] Macro to Unprotect and then Reinstate Protection on Sheets
    By AllisterB in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-13-2020, 11:08 AM
  3. Unprotect a sheet that has password protection
    By cobwebs in forum Excel General
    Replies: 1
    Last Post: 01-25-2016, 04:49 PM
  4. [SOLVED] Slow Macro selecting fields from pivot filters...help!
    By pfeifferjoey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-14-2014, 11:34 AM
  5. [SOLVED] script to selecting correct sheet calculated via cell formula
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2013, 09:42 AM
  6. Macro to unprotect sheet - run macros - then unprotect sheet is not working
    By AnnieBrownTX in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2012, 06:59 PM
  7. Allow use of filters with sheet protection?
    By javierdrm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-27-2007, 07:56 AM

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