+ Reply to Thread
Results 1 to 4 of 4

Slow VBA code: Protecting cells based on colors while changing colors based on cell change

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2012
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    2

    Slow VBA code: Protecting cells based on colors while changing colors based on cell change

    Hello! Have been reading these forums for weeks while doing some work and the helpfulness is really endless! I have found helpful bits of code that works really really well, but it's very slow! It seems like it needs to execute after EVERY change to the worksheet in order to work.

    This code, firstly, protects the worksheet and locks those cells that are color-coded other than green and yellow.

    Then, it checks if there is a change to the cell and if there is a change to a cell it recolors the cell as yellow.

    It runs very fast in a small worksheet with no data, but once I use the full Worksheet with 5 color-codes and up to 2000 rows it becomes very slow.

    Is there a faster solution, like if I limited the working range of the sheet? I really only need to protect BM2:DC2000, not the whole thing.

    Thank you for any ideas.

    Sub Worksheet_Change(ByVal Target As Range)
        'Do nothing if more than one cell is changed or content deleted
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        
        'Protect the sheet
        ActiveSheet.Protect UserInterfaceOnly:=True
        
        'For green or yellow cells, unlock. For all else, lock.
        Dim c As Range
        ActiveSheet.Cells.Locked = True
        For Each c In ActiveSheet.UsedRange
            If c.Interior.ColorIndex = 15 Or c.Interior.ColorIndex = 6 or c.Interior.ColorIndex = xlNone Then
                c.Locked = False
            End If
        Next c
        Set c = Nothing
        
        'Colorize by yellow.
        Dim isect As Range
        
        Set isect = Application.Intersect(Target, Range("BM2:DC2000"))
        
        If Not isect Is Nothing Then
        
        Target.Interior.ColorIndex = 6
        
        Else: Target.Interior.ColorIndex = xlNone
        
        End If
    
    End Sub

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Slow VBA code: Protecting cells based on colors while changing colors based on cell ch

    Add...
    Application.ScreenUpdating = False
    After your sub title
    and
    Application.ScreenUpdating = True
    Before End Sub
    See if that helps.
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Registered User
    Join Date
    02-24-2012
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Slow VBA code: Protecting cells based on colors while changing colors based on cell ch

    That did it! Thank you so much. I know I probably should have read a FAQ about this out there somewhere on optimizing your code, appreciate it tons!

  4. #4
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Slow VBA code: Protecting cells based on colors while changing colors based on cell ch

    No problem, glad it worked out for you.

+ 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. Change multiple cell colors based on date and time passed
    By cvalentine02 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-12-2013, 12:20 PM
  2. Replies: 2
    Last Post: 01-27-2012, 09:40 PM
  3. Changing colors of cells based on other cells
    By Squatty in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-17-2011, 03:09 PM
  4. Changing Cell Colors Based on Other Cell Values
    By Vinx in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2010, 07:10 PM
  5. Change Cell Fill Color Based on Colors in 2 Columns
    By djkante in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-05-2010, 08:07 PM
  6. Replies: 2
    Last Post: 08-01-2007, 07:00 PM
  7. Replies: 1
    Last Post: 02-05-2005, 02:06 PM
  8. [SOLVED] Is it possible to change colors of cells based on value in cell (.
    By LC in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-14-2005, 02:06 AM

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