+ Reply to Thread
Results 1 to 3 of 3

VBA Locking Range, if cell date value is less than today.

  1. #1
    Forum Contributor
    Join Date
    03-12-2014
    Location
    Riyadh
    MS-Off Ver
    Office 365
    Posts
    141

    VBA Locking Range, if cell date value is less than today.

    Greetings, hope all is well!

    Trying with VBA to lock range from to B:K, provided that the date of K is less than today.

    Can you please help me with a code that will be applied to the active sheet 'ranges'? and whenever new rows are filled?


    Thank you very much in advance!
    Attached Files Attached Files
    Last edited by saudi_red_neck; 03-22-2020 at 04:33 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: VBA Locking Range, if cell date value is less than today.

    Hi there. This isn't difficult, but there are a few little pitfalls to avoid.

    Firstly, to "lock" cells the worksheet needs to be protected.
    • All cells are locked by default, so this means
      • You need to decide which cells should be editable by the user
      • You need to control which cells are editable by the user
    • There needs to be a mechanism to add data to your sheet
      • To add lines to the data the worksheet needs to be unlocked first
      • data must be added in unlocked cells while the other cells remain locked to the user

    This could be accomplished by unlocking the whole columns B:K - that's rather untidy, but can be OK. What I've done in the sample, however, is convert the range of data to a table. Tables are easy to reference, and can be expanded easily.

    The actual locking is in the "Workbook_Open()" function:
    Please Login or Register  to view this content.
    This simply finds the last row in column K, iterates through the cells above it and locks those cells if the date in K is earlier than today's date.

    To add a new row to the table for more data this routine will suffice - it unprotects the worksheet, adds a new row to the table, and re-protects the worksheet afterwards:
    Please Login or Register  to view this content.
    So it's easy to see which cells are locked or unlocked I've added conditional formatting to all cells. I know this may be over-kill for what you need!

    HTH
    Tim
    Attached Files Attached Files
    Never stop learning!
    <--- please consider *-ing !

  3. #3
    Forum Contributor
    Join Date
    03-12-2014
    Location
    Riyadh
    MS-Off Ver
    Office 365
    Posts
    141

    Re: VBA Locking Range, if cell date value is less than today.

    this is very cool! thank you very much.

+ 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. [SOLVED] call up value in cell range by today's date
    By chapungmgr in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-16-2019, 02:21 AM
  2. [SOLVED] Finding Dates Within Date Range (Based on Today's Date) Whilst Matching Name?
    By EduardStoo in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 02-08-2019, 10:31 AM
  3. Highlight Cell if date range is within today
    By Noahtea in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-04-2018, 09:59 PM
  4. Highlight a date range if today's date falls within that range
    By sdarnell in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-01-2014, 10:03 AM
  5. Locking cells when today's date is passed.
    By Amir AbdRabu in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-29-2014, 09:09 AM
  6. Replies: 4
    Last Post: 09-12-2013, 11:32 PM
  7. Replies: 1
    Last Post: 09-17-2012, 01:07 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