+ Reply to Thread
Results 1 to 3 of 3

Locking cells based on color in doc with merged cells

Hybrid View

  1. #1
    Registered User
    Join Date
    07-02-2014
    Location
    Sweden
    MS-Off Ver
    Office 2013
    Posts
    17

    Question Locking cells based on color in doc with merged cells

    Hi!
    I have created a simple script that is supposed to lock all the cells that don't have a specific color. It worked fine on my test document but when I tried it on the excel project that I designed it for I got the error message "Run-time error '1004': Unable to set the locked property of the Range class." I believe that it is the merged cells are the cause of the problem. Is it possible to edit the script so it could work on merged cells as well.

    Sub Protection()
    For Each c In ActiveSheet.UsedRange
    Debug.Print c.Interior.ColorIndex
    If c.Interior.ColorIndex = 36 Then
    c.Locked = False
    Else
    c.Locked = True
    End If
    Next c
    End Sub

    Kind Regards
    Lars

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Locking cells based on color in doc with merged cells

    Hi spuds,

    I highly encourage you to use 'Option Explicit'. To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. dim i as Integer).
    http://www.cpearson.com/excel/DeclaringVariables.aspx

    Try the following tested using Excel 2003:
    Sub Protection()
    
      Dim c As Range
      Dim myMergedRange As Range
      Dim iColorIndex As Integer
    
      For Each c In ActiveSheet.UsedRange
        iColorIndex = c.Interior.ColorIndex
        Debug.Print c.Address(False, False) & "  " & iColorIndex
        
        If IsMerged(c) = True Then
          If IsTopLeftCellOfMergedRange(c) Then
            Set myMergedRange = c.MergeArea
            If iColorIndex = 36 Then
              myMergedRange.Locked = False
            Else
              myMergedRange.Locked = True
           End If
         End If
        Else
          If iColorIndex = 36 Then
            c.Locked = False
          Else
            c.Locked = True
          End If
        End If
       
      Next c
      
      'Clear object pointer
      Set myMergedRange = Nothing
    
    End Sub
    
    Function IsMerged(rCell As Range) As Boolean
      'Returns True if the input cell is part of a 'merged cell'
      IsMerged = rCell.MergeCells
    End Function
    
    Function IsTopLeftCellOfMergedRange(rCell As Range) As Boolean
      'Returns True if the input cell is the 'top left' cell of a 'merged cell'
      Dim r As Range
      Dim rStart As Range
    
      'Only process if the input cell is part of a 'merged cell'
      If IsMerged(rCell) = True Then
      
        'Get the 'top left' cell of the merged area
        Set r = rCell.MergeArea
        Set rStart = r.Cells(1, 1)
        
        'Return True if this cell is the 'top left' cell
        If rStart.Address = rCell.Address Then
          IsTopLeftCellOfMergedRange = True
        End If
      
        'Clear range objects
        Set r = Nothing
        Set rStart = Nothing
      End If
      
    End Function
    Lewis

  3. #3
    Registered User
    Join Date
    07-02-2014
    Location
    Sweden
    MS-Off Ver
    Office 2013
    Posts
    17

    Re: Locking cells based on color in doc with merged cells

    Thanks a million!
    The code worked like a charm.
    Thank you for the great link, helped a lot in improving my understanding in VBA.
    Cheers
    Lars

+ 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. Copying merged cells (3 cells) based on conentes of any of 3 cells to right
    By BobW1952 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 06-02-2014, 09:29 PM
  2. Locking cells based on the value of other cells
    By aaronp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-07-2013, 12:22 AM
  3. Locking specific cells eventually changing to locking all cells
    By davidingilbert in forum Excel General
    Replies: 1
    Last Post: 03-19-2013, 06:50 PM
  4. Replies: 1
    Last Post: 06-28-2012, 11:53 AM
  5. Replies: 2
    Last Post: 03-01-2007, 04:51 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