+ Reply to Thread
Results 1 to 4 of 4

macro to lock cell by column header based on value in another cell by column header

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2015
    Location
    Austin, Texas
    MS-Off Ver
    2010
    Posts
    6

    macro to lock cell by column header based on value in another cell by column header

    I have a sales report containing active customers from the database and prospective customers added by the salespeople.
    I want to protect the active customers' names. So, I need a macro that:
    If cell in the "Status" column does not contain the word "New Prospect" (there may be other words in this cell), then protect the cell in the "Customer Name" column.
    Naturally, it needs to search all rows. The number of rows will vary per report.

  2. #2
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: macro to lock cell by column header based on value in another cell by column header

    Hi Closet Guru,

    try this one:
    Sub LockexistingCustomers()
        Dim CustomerCol As Integer
        Dim StatusCol As Integer
        Dim i As Integer
        Dim FirstDataRow As Double
        Dim LastRow As Double
        Dim SheetPW As String
    
        '_________________________________
        'Adjust for your file here
        CustomerCol = 1    'A=1, B=2, C=3 ...
        StatusCol = 2    'A=1, B=2, C=3 ...
        FirstDataRow = 2    'The first row below your header
        SheetPW = "lock"    'Password for your worksheet
        '_________________________________
        
        'unprotect Sheet
        ActiveSheet.Unprotect SheetPW
        'unprotect all cells
        ActiveSheet.Cells.Locked = False
        'Determine the last row
        LastRow = ActiveSheet.Cells(Rows.Count, CustomerCol).End(xlUp).Row
        'Loop through all rows
        For i = FirstDataRow To LastRow
            If InStr(1, LCase(ActiveSheet.Cells(i, StatusCol)), LCase("New Prospect")) = 0 Then    'checking on lower case to avoid case sensiteveness
                'no new prospect - lock cell
                ActiveSheet.Cells(i, CustomerCol).Locked = True
            End If
        Next i
        'Protect Sheet again
        ActiveSheet.Protect SheetPW
    End Sub
    Don't forget to adjust the values in the first section to "your" values.

    Let me know if this is what you need.

    Theo
    Remember To Do the Following....
    1. Upload sample files
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  3. #3
    Registered User
    Join Date
    04-02-2015
    Location
    Austin, Texas
    MS-Off Ver
    2010
    Posts
    6

    Re: macro to lock cell by column header based on value in another cell by column header

    Thank you, that worked great. And I appreciate your notes explaining each portion of the macro.

    Was wondering if there is a way to search by the header, rather than entering the column #, as the column # may change.

  4. #4
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: macro to lock cell by column header based on value in another cell by column header

    Hi ClosetGuru,
    sure, just use the macro below (don't forget to adjust the correct Column Header Names):

    Sub LockexistingCustomers()
        Dim CustomerCol As Integer
        Dim StatusCol As Integer
        Dim i As Integer
        Dim FirstDataRow As Double
        Dim LastRow As Double
        Dim SheetPW As String
        Dim CustomerHeaderName As String
        Dim StatusHeaderName As String
        Dim rngCell As Range
        '_________________________________
        'Adjust for your file here
    
        FirstDataRow = 2    'The first row below your header
        SheetPW = "lock"    'Password for your worksheet
        CustomerHeaderName = "Customer"    'Title of the columnheader where the Customner can be found
        StatusHeaderName = "Status"    'Title of the colmnheader which keeps the status informaton
        '_________________________________
    
        'Find correct Columns
        Set rngCell = ActiveSheet.Rows(FirstDataRow - 1).Find(CustomerHeaderName, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True)
        If Not rngCell Is Nothing Then
            CustomerCol = rngCell.Column
        End If
        Set rngCell = ActiveSheet.Rows(FirstDataRow - 1).Find(StatusHeaderName, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True)
        If Not rngCell Is Nothing Then
            StatusCol = rngCell.Column
        End If
    
    
        'unprotect Sheet
        ActiveSheet.Unprotect SheetPW
        'unprotect all cells
        ActiveSheet.Cells.Locked = False
        'Determine the last row
        LastRow = ActiveSheet.Cells(Rows.Count, CustomerCol).End(xlUp).Row
        'Loop through all rows
        For i = FirstDataRow To LastRow
            If InStr(1, LCase(ActiveSheet.Cells(i, StatusCol)), LCase("New Prospect")) = 0 Then    'checking on lower case to avoid case sensiteveness
                'no new prospect - lock cell
                ActiveSheet.Cells(i, CustomerCol).Locked = True
            End If
        Next i
        'Protect Sheet again
        ActiveSheet.Protect SheetPW
    set rngCell = nothing
    End Sub
    Let me know if it works now, and don't forget to mark the thread as "Solved" if it does.

    Have a happy Easter

    Theo

+ 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. Replies: 1
    Last Post: 01-30-2014, 01:42 PM
  2. write userform data to cell based on column header
    By EMAP in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-13-2012, 08:30 AM
  3. [SOLVED] Trying to return a column header in a cell based on a value
    By brianfromla in forum Excel General
    Replies: 2
    Last Post: 06-26-2012, 10:37 AM
  4. Fill Row Header based on cell in column
    By diggerd in forum Excel General
    Replies: 2
    Last Post: 01-06-2010, 09:59 PM
  5. Lookup Column Header based on cell value
    By kingcal in forum Excel General
    Replies: 4
    Last Post: 08-13-2009, 03:56 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