Results 1 to 10 of 12

Thread: Lock Cells Based On Interior Colour Including Conditional Fomatting

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Welcome to ExcelFox!!

    Try this untested code. You have to define the color depending upon the other cells.

    Code:
    Dim Cell As Range
    Dim mergedRange As Range
    
    For Each Cell In ActiveSheet.Range("A1:Y160")
      If Cell.MergeCells = False Then
        Select Case True
          Case Cell.Interior.ColorIndex = 15
               Cell.Locked = True
          Case Else
               Cell.Locked = False
        End Select
      Else
        Set mergedRange = Cell.MergeArea
        
        Select Case mergedRange.Address(0, 0)
            Case "F6:O6"
                If Range("L4").Value = "Whatever" Then
                    mergedRange.Interior.ColorIndex = 15    'adjust the color index
                    mergedRange.Locked = True
                Else
                    mergedRange.Interior.ColorIndex = 15    'adjust the color index
                    mergedRange.Locked = True
                End If
            Case "Q126"
                'same as above
            
            Case Else
                Select Case True
                  Case mergedRange.Interior.ColorIndex = 15
                       mergedRange.Locked = True
                  Case Else
                       mergedRange.Locked = False
                End Select
      End If
    Next
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0
    Thanks for the reply (and welcome!).

    If possible, I would prefer to avoid doing single-cell/mergearea specifications in the code. The form is quite complex, with a not-insignificant number of questions, and a significant number of those have impacts on other questions within the form. I already have too many absolute-references in some of the other code, which makes updating in when additional questions are inserted a real chore. (I realise that seeing the form would probably help explain all this, but I am still waiting on an answer from my employer).

    If there is no other way, I will look at switching all of the conditional formatting into the code, but I was really hoping that wouldn't be necessary...

Similar Threads

  1. Replies: 4
    Last Post: 07-04-2013, 01:56 PM
  2. Lock Cells After Data Entered
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 4
    Last Post: 06-28-2013, 10:52 PM
  3. Nth Working Day Including Saturday
    By Excel Fox in forum Download Center
    Replies: 0
    Last Post: 10-10-2012, 02:41 AM
  4. Conditional Format Based On Percentage Variance
    By srizki in forum Excel Help
    Replies: 3
    Last Post: 10-09-2012, 03:28 AM
  5. Lock cells on the basis of date VBA
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 22
    Last Post: 09-27-2011, 03:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •