+ Reply to Thread
Results 1 to 7 of 7

Copying Conditional Formatting when cells are/aren't protected

  1. #1
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Copying Conditional Formatting when cells are/aren't protected

    Hi,

    I know there are many issues with conditional formatting when users cut/paste or insert rows. What I'm trying to do is copy only the conditional formatting from a hidden sheet ("ParticipationLevelValues") to a target sheet the users are using. All the cells on the hidden sheet are unlocked as the default.

    When the user saves the file is when my code executes. This way when they open the file the next time, all the conditional formatting rules have been reset. The spreadsheet is 78 columns wide and might run 12,000 rows deep.

    When the user hits a "SAVE" button, the macro unprotects the sheet and checks the validity of the data entries before resetting the conditional formatting rules. After this, then the macro protects the sheet and then saves the file.

    Please Login or Register  to view this content.
    The above code sort of meets my needs as the conditional formatting plus all the other formatting gets copied. The problem is some of the cells in the target sheet are locked via Worksheet_Change(ByVal Target As Range) during data entry depending on the value of the cell (if the cell is "Yes", it gets locked; otherwise, it is unlocked).

    If a cell is locked and I use the code above, the conditional formatting that gets copied over is correct, but the previously locked cells in the target now become unlocked. I do not want the format cell protection to be changed.

    Is there any way if a cell is locked on the target sheet, to apply the conditional formatting and still leave it locked. The same goes if a cell is unlocked on the target sheet, to apply the conditional formatting and still leave it unlocked.

    Is there a way to copy/paste just the conditional formatting rules?

    I thank you in advance for any assistance or suggestions you have.
    Last edited by Murman01; 04-19-2023 at 12:30 PM.

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copying Conditional Formatting when cells are/aren't protected

    One option is to include the protect/unprotect in the code while unlocking the cells the user needs to access. Furthermore, the conditional formatting can be added directly through code without the need for copying cells. Here is an example of both:

    PHP Code: 
    Sub Protect_Unprotect_AddConditionalFormating()
    Dim c1 As FormatConditionrng As Range
    ActiveSheet
    .Unprotect
        Set rng 
    ActiveSheet.Range("K2:K100")
        
    Set c1 rng.FormatConditions.Add(Type:=xlExpressionFormula1:="=AND(K2>0,L2=""y"")")
        
    c1.Interior.Color 11854022
        ActiveSheet
    .Range("A2:T149").Locked False
    ActiveSheet
    .Protect AllowFormattingColumns:=TrueAllowFormattingRows:=True
    End Sub 

  3. #3
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Re: Copying Conditional Formatting when cells are/aren't protected

    I should have made it clear that when the user saves the file is when my code executes. This way when they open the file the next time, all the conditional formatting rules have been reset. The spreadsheet is 78 columns wide and might run 12,000 rows deep.

    When the user hits a "SAVE" button, the macro unlocks the sheet and checks the validity of the data entries before resetting the conditional formatting rules. After this, then the macro locks the sheet and then saves the file. I presumed the most efficient method would be to copy/paste conditional formatting from the source sheet than to hardcode the conditional formatting for each column as you have suggested.

    Is there a way to copy/paste just the conditional formatting rules?

  4. #4
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Re: Copying Conditional Formatting when cells are/aren't protected

    Thank you very much for your assistance as it got me on a better path.

    However, now I get a "Run-time error '1004': Application-defined or object-defined error" for some reason in the following code at the "MyRange.FormatConditions.Delete" line and I don't understand why.

    PHP Code: 
    Public Sub FormatRow()
     
    Dim MyRange As Range

        Set MyRange 
    ActiveSheet.Range("A9:BG12500")
        
    MyRange.FormatConditions.Delete

        
    'First rule highlights the active cell in pink while the second rule highlight's the active cell's row and column in blue.
            '
    First CF Rule
            MyRange
    .FormatConditions.Add Type:=xlExpressionFormula1:="=AND(CELL(""col"")=CELL(""col"",A9),CELL(""row"")=CELL(""row"",A9))"
            
    MyRange.FormatConditions(1).Interior.Color RGB(255204255'Light pink
            MyRange.FormatConditions(1).StopIfTrue = False

            '
    Second CF Rule
            MyRange
    .FormatConditions.Add Type:=xlExpressionFormula1:="=OR(CELL(""col"")=CELL(""col"",A9),CELL(""row"")=CELL(""row"",A9))"
            
    MyRange.FormatConditions(2).Interior.Color RGB(218238243'Light blue
            MyRange.FormatConditions(2).StopIfTrue = False
            
            '
    Third CF Rule
            
    '....etc.
    End Sub 

  5. #5
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copying Conditional Formatting when cells are/aren't protected

    If you are going to remove all the format conditioning on the sheet, you can use:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Re: Copying Conditional Formatting when cells are/aren't protected

    Even when I try that suggested piece of code to remove all the format conditioning on the sheet, I still get the "Run-time error '1004': Application-defined or object-defined error" message.

    PROBLEM SOLVED: I needed to unprotect the worksheet before running the code to avoid the 1004 error. Duh! Somehow the piece of code to unprotect was commented out. I couldn't see the trees through the forest...my fault. My last piece of code works if I unprotect it at the start and then protect it at the end of the subroutine.
    Last edited by Murman01; 04-25-2023 at 11:24 AM.

  7. #7
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Copying Conditional Formatting when cells are/aren't protected

    Removed after testing my suggestion.
    Last edited by Croweater; 04-21-2023 at 02:58 AM.

+ 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] Copying conditional formatting to other cells
    By bmo7 in forum Excel General
    Replies: 5
    Last Post: 07-16-2014, 10:22 AM
  2. Replies: 2
    Last Post: 05-14-2014, 09:14 AM
  3. [SOLVED] How to avoid copying Conditional Formatting to other cells
    By amarus99 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-20-2012, 02:45 PM
  4. Copying Conditional Formatting to Adjacent Cells
    By arsenal123gunners in forum Excel General
    Replies: 1
    Last Post: 06-24-2010, 09:24 PM
  5. Conditional Formatting to highlight protected cells
    By lostinformulas in forum Excel General
    Replies: 1
    Last Post: 02-16-2009, 02:52 PM
  6. copying conditional formatting to relevant cells
    By karl1985 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-05-2007, 10:47 AM
  7. Copying cells with conditional formatting
    By Benfanfromlo in forum Excel General
    Replies: 3
    Last Post: 02-10-2005, 03:06 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