Results 1 to 3 of 3

Formatting/locking/Unlocking range of cells

Threaded View

  1. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello SSGMiami,

    The attached workbook contains 2 macros to automatically gray out and lock the cells that aren't in the current month of the date in cell "C3". The macro locks the worksheet but is not password protected. Password protected can be added if you want it.

    Main Macro
    Sub Macro1()
    
      Dim ColLeft As Long
      Dim ColRight As Long
      Dim D As Integer
      Dim EOM As Integer
      Dim LastRow As Long
      Dim Rng As Range
      Dim StartRow As Long
      
        StartRow = 7
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
        LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
        
          ActiveSheet.Unprotect Password:=""
          Cells(3, "C").Locked = False
          EOM = Day(DateSerial(Year(Range("C3")), Month(Range("C3")) + 1, 0))
          D = Day(Cells(3, "C"))
          
             If D - 8 < 0 Then
                ColLeft = D - 1
             Else
                ColLeft = D - (D - 7)
             End If
             
             If D + 8 > EOM Then
                ColRight = EOM - D
             Else
                ColRight = EOM - (D + 7)
             End If
             
              Set Rng = Range(Cells(StartRow, "C"), Cells(LastRow, "Q"))
                With Rng.Cells
                  .Locked = True
                  .Interior.ColorIndex = 15   'Light Gray
                End With
             
              Set Rng = Range(Cells(StartRow, 10 - ColLeft), Cells(LastRow, 10 + ColRight))
                With Rng.Cells
                  .Locked = False
                  .Interior.ColorIndex = xlColorIndexNone
                End With
            
          ActiveSheet.Protect Password:=""
          
    End Sub
    Worksheet Event Macro
    Private Sub Worksheet_Change(ByVal Target As Range)
    
      If Not Intersect(Range("C3"), Target.Cells(1, 1)) Is Nothing Then
        Call Macro1
      End If
      
    End Sub
    Sincerely,
    Leith Ross
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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