+ Reply to Thread
Results 1 to 4 of 4

VBA code for locking and unlocking cells depending on another cells value

  1. #1
    Registered User
    Join Date
    05-19-2020
    Location
    London, England
    MS-Off Ver
    Windows 10 Pro
    Posts
    3

    VBA code for locking and unlocking cells depending on another cells value

    Hi All

    I have never used VBA before and am finding it a complete mind field!

    I need a VBA code that will lock/unlock a selection of cells depending on the value of another cell.

    Basically if the value of cell O15 is greater than 04:30 (4 and a half hours in time) then it unlocks cell range Q8:Q14 and turns the cells from filled Black in colour to filled White, but if the value of cell O15 is less than 04:30 (4 and a half hours in time) then cell range Q8:Q14 must stay locked and stay filled black.

    Is anyone able to assist with this?
    Many thanks in advance

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: VBA code for locking and unlocking cells depending on another cells value

    Is the value in O15 based on a formula, or manually entered? That will determine if you use a Worksheet_Calculate or Worksheet_Change event. I've shown the Calculate event below.
    Please Login or Register  to view this content.
    To add this code, right-click the sheet tab and select View Code. Paste this code into the VB Editor window for that sheet and then close the VB Editor. Whenever the worksheet calculates, it will check cell O15 and adjust Q8:Q14 accordingly.

    If you are using manual entry for O15, the code would be more like:
    Please Login or Register  to view this content.
    Note: 0.1875 is the numeric equivalent to 4:30am based on # of seconds elapsed in the day (16200) divided by total # of seconds in a day (86400).

  3. #3
    Registered User
    Join Date
    05-19-2020
    Location
    London, England
    MS-Off Ver
    Windows 10 Pro
    Posts
    3

    Re: VBA code for locking and unlocking cells depending on another cells value

    Hi Paul

    Thank you for the code, it does work, however I'm now thinking it may just be easier to hide the whole of column Q, but unhide it if O15 is greater than 4:30.
    Is this possible to do?

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: VBA code for locking and unlocking cells depending on another cells value

    Yes, just replace the "Range("Q8:Q14")...." bits with

    Range("Q:Q").EntireColumn.Hidden = True (to hide the column)

    or

    Range("Q:Q").EntireColumn.Hidden = False (to show the column).

+ 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. Locking / Unlocking Cells
    By almulder in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-23-2020, 04:00 PM
  2. Locking or Unlocking validated cells depending on the previous cell
    By biajw in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-05-2014, 11:36 AM
  3. Locking/Unlocking Cells By Checking Other Cells
    By brandylee8288 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2013, 05:25 PM
  4. Locking and unlocking of cells
    By srinivassurapareddi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 02:19 AM
  5. Locking and Unlocking Cells
    By vikas.bhandari in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2010, 10:27 AM
  6. Formatting/locking/Unlocking range of cells
    By SSGMiami in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2009, 09:00 AM
  7. Unlocking/Locking Cells with a button
    By No.Solutions. in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-22-2008, 11:27 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