Clear Contents of unprotected Specific Range in a protected sheet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • auhom
    New Member
    • Jul 2016
    • 5

    Clear Contents of unprotected Specific Range in a protected sheet

    I want to clear the unprotected contents of a protected sheet within a specific range. for example when i run VBA it will clear the contents ranging from A1002:F1301 and G1002: AZ1301. The mentioned ranges are unprotected. I want only the cell between these range to ne clear but not format or any other change . How can i do this ?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    I am a little confused about you request. From my interpretation you wish to clear all Cells within a specified, unprotected Range except a single Cell within that Range. If this is so, then the following Macro will do the trick, it will clear all Cells within the Range $A$1002:$F$1301 except $C$1016.
    Code:
    Dim rng1 As Excel.Range
    Dim rng2 As Excel.Range
    
    Set rng1 = Worksheets("Sheet1").Range("A1002:F1301")
    
    For Each rng2 In rng1
      If rng2.Address <> "$C$1016" Then
        rng2.ClearContents
      End If
    Next

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      ADezii, I think a typo here, "cell" might have been "cells" given that these are adjacent ranges.
      Code:
      Sub poc()
          ThisWorkbook.Worksheets("Sheet1").Range("A1002:AZ1301").ClearContents
      End Sub
      will do the trick.

      If this isn't the case then auhom needs to clarify the question.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Thanks zmdb for the clarification. For my own curiosity, what is your opinion on using?
        Code:
        ActiveWorkbook vs. ThisWorkbook

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          I personally have two to 15 different workbooks open at a time at work and having ran afoul of executing code and having the wrong workbook effected, I tend to use "ThisWorkBo ok" over "ActiveWorkbook ".

          Comment

          • MikeTheBike
            Recognized Expert Contributor
            • Jun 2007
            • 640

            #6
            Hi zmbd

            I very rarely use ThisWorkbook as it will refer to the workbook in which the running code is written. I hardly ever read or write info to/from the workbook that is running the code.

            From memory I only use ThisWorkbook for checking that the user has not selected this file to open ie = ThisWorkbook.Na me (in addition to checking the selected file is not already open).

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              @zmbd and Mike:
              Thanks for tour input.

              Comment

              Working...